Variables In JavaScript🤩🤩
Beginner’s guide to creating, naming and storing values in variables.
Hi everyone! Today we’re going to be discussing one of the basics of JavaScript.
Variables are like containers, they are used to hold information we’ll need when programming.
How to create a variable in JavaScript.
Declaring variables in JavaScript
Creating a variable is also called declaring a variable (these terms are used interchangeably). There are three ways to create a variable in JavaScript. Variables are created using one of the following keywords
- let
- var
- const
For example:
var variableName;
let variableName;
const variableName;
Var
is an old way of declaring variables a more recent way of declaring variables is using let
Naming variables in JavaScript.
Rules for naming variables in JavaScript
When naming your variables put some thought to making the names descriptive and easily understandable. This will make your program easy to read and understand in the future when you have to refactor it. Here are some rules you should look out for when naming your variables:
Variable names should begin with either a letter or an underscore or a dollar sign. Example:
let $house1; let _house2; let house3; let House4;
Variable names should not begin with numbers or special characters except the dolla sign. For example
let 1house;
is invalid.Variable names should not be keywords. Keywords cannot be used as variable names.
Also note that variable names are CASE SENSITIVE. This means that let boy;
is not the same as let Boy;
. The two variable created will be treated as different because their casing is different. To ensure consistency in naming your variables you can adopt one of the following naming conventions in naming your variables.
let payPal; // camel case
let PayPal; // Pascal case
let iteration-count; // kebab case
let Iteration_count; // snake case
camel case and pascal case are often the most popular used convention in writing JavaScript programs. The kebab case is not recommended as it is not a common practice in writing JavaScript programs
How to store data in a variable.
Assigning values to a variable.
Storing data in a variable is also called assigning a value to a variable. To store data in a variable(assign value to a variable), use the =
symbol.
Place the variable name on the left side of of the =
symbol and place the value you want to store in the variable goes on the right side of the =
symbol.
Here are a few examples:
let maleName; // variable created
maleName = Max; // value is assigned
var femaleName; // variable created
femaleName = Phoebe // value is assigned
const acceleration; // variable created
acceleration = 9.8; // value is assigned
The =
symbol is called the assignment operator .
Variables can be created before assigning values to them. Values can also be assigned to variables at the moment of creating them. Creating variables and assigning values to them at the same time is known as initializing a variable and here’s how to initialize a variable. .
let maleName = Max; // variable initialized
var femaleName = Phoebe; // variable initialized
const acceleration = 9.8; // variable initialized
That’s a wrap! I hope you’ve learnt something new today