Loading Please Wait...
JavaScript variables are used to store data and values.
All JavaScript variables must be identified with unique names. These unique names are called identifiers.
Rules for defining names for variables (unique identifiers) are:
Creating a variable in JavaScript is called "declaring" a variable.
4 Ways to Declare a JavaScript Variable:
var a;
let b;
const c = a + b;
// Multiple declaration in single line
var x, y, z;
After the declaration, the variable has no value (technically it is undefined)
a = 5;
b = 3;
c = a + b;
// Multiple assignment in single line
x = 3, y = 2, z = x + y;
var a = 5,
b = 5,
c = a + b;
All var, let and const are used to declare variables but all serve different purpose.
# | var | let | const |
---|---|---|---|
Assignment | Not necessary | Not necessary | Must assign value when declare |
Usability | Can be used before declaration | Must declare before use | Must declare before use |
Re-declare | Yes | No | No |
Re-assign | Yes | Yes | No |
Scope | Global | Block | Block |
When To Use | Use this if you want a variable to access anywhere in the program (globally) | Use this if you do not want a variable to re-declare even outside the block scope | Use this when you know that the value must be fixed like (arrays, objects, functions, regex, etc) |
var a; // valid
a = 5; // valid
let b; // valid
b = 5; // valid
const c; // invalid
c = 10; // invalid
const c = 10; // valid
var a; // valid
var a = 5; // valid
let b; // valid
let b = 5; // invalid
const c = 5; // valid
const c = 5; // invalid
var a = 5; // valid
a = 10; // valid
let b = 10; // valid
b = 15; // valid
const c = 20; // valid
c = 30; // invalid
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved