Javascript Variables

In JavaScript, variables are used to store data values. They are declared using the var, let, or const keywords.

Syntax:

var variableName = value;
let variableName = value;
const variableName = value;

Example:

var x = 5;
let y = "Hello";
const z = true;

In the previous illustration, x is a number variable with the value of 5, y is a string variable with the value of “Hello,” and z is a boolean variable with the value of true.

Example:

var a = 10;
var b = a;
console.log(b); // Output: 10

The value of a, which is 10 in the case above, is given to b.

As demonstrated in the example below, variables can also be used to store reference data types like arrays and objects:

var myArray = [1, 2, 3];
var myObject = { name: "John", age: 30 };

In the example above, myArray is a variable that stores an array with the values 1, 2, and 3, while myObject is a variable that stores an object with the property’s name and age.