JavaScript Variables

Javascript Variables

Variables are the first thing that you have to learn in any programming language. In this article, we will be talking about javascript variables with examples.

javascript variables

What are Javascript Variables?

Variables work like a container for storing data. It also can be changed depending on the condition of a program.

There are four ways to declare variables, they are –

1. Using Var

In this method, we declare a variable with var symbolic name.

var a = 6;
var b = 8;
var c = a + b;

In this example, a, b and c are the variables declared with var.

2. Using Let

Let is also a reference to declare a variable.

let a = 7;
let b = 8;
let c = a + b;

Same as before, here a, b, and c are the variables. And those values can be changed.

3. Using Const

Const is one of the most popular symbolic names for declaring variables in javascript.

const sub1 = 3000;
const sub2 = 2000;
let totalSub = sub1 + sub2;

Here we have declared sub1 and sub2 variables with the const keyword. So, we can not change the value of that two variables. But after that totalSub is declared with let and it can be changed.

Also Read: Introduction to JavaScript Hoisting

4. Using Nothing

We can also use nothing to declare a variable in javascript.

a = 3;
b = 4;
c = a + b;

Variable Scope

Variable Scope is the place of your code or program where it is defined. Javascript Variables have only two types of Variable Scope.

  • Global Variable – A global can be created outside the function. So a global variable is having global scope. It also can be defined from anywhere in your program. We also can access it from anywhere.
  • Local Variable – We can create a local variable inside a function so it is having a local scope. And, we can not access its parameter outside the function.

Naming Javascript Variables

There are some limitations to naming variables in javascript. Here we are going to discuss the rules.

  • We can’t use space in variables.
  • We can start a variable using letters, underscore(_), or a dollar sign($).
  • The name of a variable can only contain letters, numbers, underscores, or dollar signs.
  • The variable names are case-sensitive.
  • We can not use certain words in a variable name. Because they have another use in the javascript program.

what is the difference Between var and let?

var and let both are used to declare the variable in javascript, but there are some differences between var and let.

First of all, var allows you to redeclare a variable, whereas let does not permit you to redeclare a variable.

Furthermore, let doesn’t support hoisting, but hoisting occurs in var.

If we declare a variable using var and do not assign any value to the variable it will return undefined. But, when we declare a variable using let in a similar way, it will return a reference error.

Summing Up

In this session, we have learned about javascript variables, how to declare them, and the naming of javascript variables. Variable defining has a lot behind their simple declaration as time complexity depends on the type of operator used to define (opens in a new tab) a variable.

If you have any queries related to this article, feel free to comment down below.

IndGeek provides solutions in the software field, and is a hub for ultimate Tech Knowledge.