Variables in JavaScript

Variable in JavaScript is a container. Just as we follow some rules when speaking English (grammar), we must follow some rules when writing a JavaScript program. The set of these rules is called syntax in JavaScript.

What is a variable in JavaScript?

A variable is a container that stores a  data value. This is very similar to the containers used to store rice, water, and oats (think of this as an analogy!)

How to declare variables

Before you operate a variable in a JavaScript program, you need to declare it. Variables are declared with the var, let, and const keywords as follows. (We will learn letconst, and var in detail in another article.)

// Variable Declaration
var firstName;
var secondName;

// We can also declare multiple variables by single var keyword
var firstName, secondName;

Assigning a value in a variable is called variable initialization. You can perform variable initialization at variable creation time or at a later time when you need that variable.

For Example, you would possibly create a variable named firstName and assign the value “Varun” to it later. For another variable, you may assign a value at the time of initialization as follows.

// Initialization of variable with a declaration
var firstName = "Varun"
var lastName;
lastName = "Dhawan"  // Initialization of variable after declaration

Naming Rules of variable

  1. The name must begin with a letter (a – z or A – Z), an underscore (_), or a dollar sign ($).
  2. You cannot begin a variable with a number.
  3. After the first letter, we can use the number (0 – 9), for example, value1.
  4. You cannot use JavaScript keywords as variable names.
  5. In JavaScript, recursive and RECURSIVE are different variables. that is, it is case-sensitive.
var 1val; // Invalid syntax
var val1; // Valid syntax
var _val; // Valid syntax
var function; // Invalid because function is reserved word(keyword)in Javascript

Types of variables

  • Global variables

A JavaScript global variable is created outside the function or created with a window object. It can be accessed from any function.

Let’s see the example of a global variable in JavaScript.

var num1 = 10;
function display(){
   console.log(num1); // We can access num1 inside display()
}
display(); // It will display 10

  • Local variables

 A local variable in JavaScript is created inside a block or function. Local Variable is accessible inside the function or blocks only.

Let’s see the example of a local variable in JavaScript

var num1 = 10; // Global variable
function display(){
   var num2 = 20; // Local variable
   console.log(num1 + " " + num2); // We can access num1 inside display()
}
display(); // It will display 10 and 20
console.log(num2); // We can't access num2 outside display()

 

Thank you so much for reading,

Happy coding

You can visit MDN Docs for more details

Some more articles on JavaScript

  1. Introduction to JavaScript
  2. Let var const in JavaScript
  3. JavaScript Data Types

 

Leave a Reply

Your email address will not be published. Required fields are marked *