JavaScript Variables

In this lesson, we will learn about variables and how to declare and initialize them. Variables are used to store data in memory for later recall or update. Variables can be accessed by their name.

Variables are containers or boxes defined with specific names, used to store different types of data (such as a username, age, etc.) for later access when needed. These values are accessed using the name with which the variable was declared.

How to create variables in Javascript

Step 1: Declaring the Variable

Variables must first be declared before they can be used. Variables can be declared using the let keyword followed by a suitable variable name that expresses the value to be stored within the variable. See the following example:

javascript
          let name;
        

In this example, I declared a variable using the let keyword followed by the variable name.

The variable name must be unique to avoid problems. Declaring two variables with the same name will result in syntax errors. See the following example:

javascript
          let name;
let name;
        

In this example, we encountered a problem because the first variable and the second variable have the same name.

We can declare two variables with different names:

javascript
          let firstName;
let lastName;
        

We can declare more than one variable using the same keyword:

javascript
          let name, age;
        

In this example, I declared two variables with the let keyword and separated the first and second variables with a comma.

This method is preferred if you believe the stored value will be subject to change later, and it is recommended.

Step 2: Assign a value to the variable.

The value can be assigned after the declaration

javascript
          let firstName;
firstName = 'Joy';
console.log(firstName);
        

It can be assigned with the declaration in a single line:

javascript
          let firstName = 'Joy';
console.log(firstName);
        

Variables can also be declared using the var keyword followed by the variable name. See the following example:

javascript
          var age;
        

In this example, I declared a variable using the var keyword followed by the variable name.

This method was the only method used for declaring variables before ES2015.

It is not currently recommended. Due to its peculiarities, such as how it handles scope (we'll learn more about that later),

Variables can also be declared using the const keyword followed by the variable name. They must be assigned a value at the same time. See the following example:

javascript
          const age = 20;
        

In this example, I declared a variable using the const keyword followed by the variable name.

This method is used if the value stored in the variable cannot be changed later.

Note: If you assign a value to the const keyword, you cannot change the value again.

Look at the following example:

javascript
          const name = 'Jack';
name = 'Jeff';
        

Rules for Naming Variables

JavaScript has specific rules for naming variables to ensure that your code runs correctly and is easy to understand. Follow these guidelines to avoid errors:

The variable name must begin with a letter, a dollar sign ($), or an underscore (_).

javascript
          let name = 'Jack'; // valid
let $name = 'Jack'; // valid
let _name = 'Jack'; // valid
        

Variable name in JavaScript can contain letters, numbers, or symbols such as a dollar sign ($) or an underscore (_).

javascript
          let first_Name = 'Jack'; // valid
        

The variable name in JavaScript cannot start with a number.

javascript
          let 1name = 'Jack'; //invalid
        

Variable names are case-sensitive, meaning age and Age are separate variables.

javascript
          let userName = 'Jack';
let UserName = 'Jeff';
console.log(userName);
console.log(UserName);
        

It is preferable to use the camelCase naming convention, where the first word starts with a lowercase letter and each subsequent word starts with an uppercase letter (with no spaces between words).

javascript
          let lastName = 'James';
        

Do not include spaces in the variable name (variable names cannot contain spaces).

javascript
          let user name = 'Jack'; //invalid
let userName = 'Jack'; //valid
        

There are reserved words in JavaScript that cannot be used as variable names. For example, the word return cannot be used.

javascript
          let return = 'value'; // Invalid
        

Conclusion

In summary, variables are essential building blocks in programming. They allow you to store, update, and manage data efficiently.