Variables are used to store and manipulate data. They are containers for holding values, and the values can be of various types such as numbers, strings, Boolean values, arrays, functions, etc.
There are 3 ways of declaring variables:
Var is the oldest way to declare variables. It has function-scope or globally scoped . They are accessible throughout the function in which they are defined or globally if declared outside of any function.
Note: Var can be redeclared
var variable_name = variable_value;
var num1 = 5;
var num2 = 6;
var product = num1 * num2;
Let is a keyword used to declare variables which has block-scope and it was introduced in ECMAScript 6, 2015. They are accessible only within the block in which they are defined, whether it's a loop, conditional statement, or any other block
let variable_name = variable_value;
let num1 = 5;
let num2 = 6;
let product = num1 * num2;
The const keyword is used to declare variables whose value cannot be changed and it has a block-scope.
It was introduced in ECMAScript 6, 2015.
const variable_name = variable_value;
Example:
const num = 1;
In JavaScript there are total 8 Values:
let age = 23;
let str = "Coder";
let boolValue = false;
let nullValue = null;
let undefinedValue = undefined;
let symb = Symbol("unique");
let userObject = { name: "Wasif", age: 23 };
let arraysValue = [1, 2, 3, 4];