JavaScript Variables

Variable are important aspects of any programming language. Variables in javascript are named container, which is used for storing information. In javascript, the variable data can we assign, modify, and update with the help of the variable name. Use a var keyword to create a new variable and then an assign(=) operator to assign a value of your choice.

Examples of Javascript variable

var name = "Jhon";
var age = 25;
var height = 6.1;

The variable name holds a string value in the above example, whereas variable age and height stores a number and Float value, respectively. Javascript is an untyped language means it doesn’t require to mention the data type of the variable.

ES6 let and const Keywords

Before 2015, Var is used to declare a variable in JavaScript. But the ES6 version of javascript allows the use of const keyword and let keyword to define a variable. The const keyword is used to define a variable that cannot be reassigned, and the let keyword-only allows you to define a variable with restricted scope.

Example of let and const

// Declaring let variables
let name = 'Rohan';
let age = 18;
// Declaring constant
const yogaDay = 'June 21';

If you reassign value to the variable yoga day, it will cause an error.

const yogaDay = 'June 21';
yogaDay = 'June 24'; // error, can't reassign the constant!

Leave a Reply

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