7554425D-0054-440D-B95E-D2ABC13D62CD Created with sketchtool.
{body}
Your Saved Info Packages

View your saved Course or Program Packages containing pricing and detailed curriculum.

Speak with a Learning Advisor.

Have any questions? We'll call you.

Fill out the form below and a Learning Advisor will reach out at a time convenient for you.

Please pick a valid date and time between 9 AM and 8 PM eastern (Monday to Friday)

By clicking "Book a call," you accept our Terms and will also receive exclusive offers and updates about new courses, workshops and events.

JavaScript Tutorial

JavaScript Variables

Ready to start your career in Development? Find out more about BrainStation's Web Development Bootcamp

JavaScript empowers millions and millions of websites and applications. While implementing such websites, it is important to organize your information and store data. Variables are a way of storing information or data in memory and giving it a name to easily remember where the information or data is stored.

Imagine, building an ecommerce website where you have to store product information. For each product, there must be a need to store the product name, manufacturer, date launched, quantities available and price. Now as a Developer, once you decide to store all this information, it becomes more and more difficult to remember where each piece of information is stored in memory because memory addresses on a computer are a long sequence of numbers and characters and it is impossible for any human to memorize them. Variables are used to name these memory locations so that when the information is needed again, one can just use the variable name and the information will be retrieved.

Variables in JavaScript can be created in three different ways using keywords var, let and const. Initially, variables were created using only the var keyword. But due to some historic drawbacks of using var, let and const were implemented for creating variables. It is recommended that Developers don’t use the var keyword anymore to create variables. Irrespective of which keyword you use to create a variable, the syntax still remains the same.

```
<keyword> <variable_name> = <value_to_be_stored>
```

As seen above, a variable can be created by specifying the keyword, followed by a variable name that will be used to store the value and as well retrieve it later, an equal sign for assigning values to the variable and the value to be stored itself.

Variable Naming Convention

Variables are powerful programming constructs. However, there is a convention that should be followed by creating a variable in JavaScript. Following are the rules that a Developer should follow while creating a variable:

  • Variable name should only contain alphabets, numbers, $ and _
  • Variable name should not start with a number
  • Variable names are case-sensitive i.e. result and Result are two different variables
  • Variables can’t be named as one of the reserved keywords like let, return, const, etc.
  • Variable should use camelCase i.e. numOne is preferred over NumOne or numone
  • Variable can’t have hyphen - in it’s name
  • Use easy to understand names that symbolize the value stored in variables. For e.g. instead of calling a variable phNum, a Developer can call it phoneNumber.
  • Don’t use single letter variable names like x, a, z, etc.

Var Keyword

Variables can be created using the var keyword. The only thing to remember is that JavaScript won’t complain or throw an error if a variable is being used before it is declared using the var keyword. In modern programming using JavaScript, using the var keyword for variables is discouraged and should be replaced with let or const keywords. Variables created using var keyword are also function-scoped or global-scoped i.e. it is very hard to limit where in a large block of code the variable should be accessible. Hence, code written using the var keyword is hard to maintain.

```
var numOne = 20;
var numTwo = 30;

var result = numOne + numTwo;
console.log(‘Result is: ‘, result);
```

Let Keyword

let keyword was introduced to solve hoisting issues that the var keyword had. let variables are block scoped and are only accessible to where they are declared. This limits the issues of variables being overwritten somewhere else in the code. Apart from this, the variables created using the let keyword follow the same syntax as the ones created using the var keyword. Variables created using let and var keywords can be reassigned a value of different kind. Hence they are mutable.

```
let numOne = 20;
let numTwo = 30;

var result = numOne + numTwo; 
console.log(‘Result is: ‘, result); // should print 50

numThree = 60;
result = numOne + numThree; // reassign result new value
console.log(‘Result is: ‘, result); // should print 80
```

Const Keyword

Sometimes, variables created should not change the value assigned to it. This can’t be achieved if variables are created using let and var keywords. In such cases, a variable should be created using the const keyword. A variable created using const can’t change the value assigned to it. It symbolizes constants.

```
let numOne = 20;
let numTwo = 30;

const result = numOne + numTwo; 
console.log(‘Result is: ‘, result); // should print 50

numThree = 60;
result = numOne + numThree; // this is not allowed as result is a constant variable
console.log(‘Result is: ‘, result); // this will not be executed as above line has error
```

const variables are also named differently sometimes when they store a value that would be otherwise hard to remember or store. Like private keys, colors, fonts, etc. usually have complex values and hence const are appropriate for it.

```
const LIGHT_GRAY = ‘#ccc’;
const DARK_GRAY = ‘#eee’;
```
Set Password

You already have an account with BrainStation, but you still need to set up a password.