HomeSoftware DevelopmentJavaScript Variables - GeeksforGeeks

JavaScript Variables – GeeksforGeeks


Variables are the constructing blocks of any programming language. In JavaScript, a variable can be utilized to retailer reusable values. These values may be something from Quantity, Boolean, String, and even a worth returned from a operate. The values of the variables are allotted utilizing the project operator(“=”). 

There are some primary guidelines to declare a variable in JavaScript:

  • These are case-sensitive
  • Can solely start with a letter, underscore(“_”) or “$” image
  • It may possibly include letters, numbers, underscore, or “$” image
  • A variable identify can’t be a reserved key phrase.

JavaScript is a dynamically typed language so the kind of variables is determined at runtime. Due to this fact there is no such thing as a have to explicitly outline the kind of a variable. We will declare variables in JavaScript in 3 ways:

Syntax:

var geek = "Good day Geek"        // declaration utilizing var
let $ = "Welcome"            // declaration utilizing let
const _example = "Gfg"        // declaration utilizing const   

All three key phrases do the fundamental process of declaring a variable however with some variations Initially, all of the variables in JavaScript had been written utilizing the var key phrase however in ES6 the key phrases let and const had been launched.

Instance 1: On this instance, we are going to declare variables utilizing var

Javascript

var a = "Good day Geeks"

var b = 10;

var c = 12;

var d = b + c;

console.log(a);

console.log(b);

console.log(c);

console.log(d);

Output:

Good day Geeks
10
12
22

Instance 2: On this instance, we are going to declare variables utilizing let 

Javascript

let a = "Good day learners"

let b = "becoming a member of";

let c = " 12";

let d = b + c;

  

console.log(a);

console.log(b);

console.log(c);

console.log(d);

Good day learners
becoming a member of
 12
becoming a member of 12

To study extra about JavaScript let verify this text JavaScript Let

Instance 3: On this instance, we are going to declare the variable utilizing the const key phrase

Javascript

const a = "Good day learners"

console.log(a);

  

const b = 400;

console.log(b);

  

const c = "12";

console.log(c);

  

c = "new"

console.log(c)

Output:

 

Clarification: const key phrase is used after we assign a worth completely to a variable. So after we attempt to change the worth of a variable declared with the const key phrase it can throw an error. The variables declared with var and let are mutable that’s their worth may be modified however variables declared utilizing const are immutable. 

To study extra about JavaScript const verify this text JavaScript Const

Be aware: The newly launched key phrases let and const are block scoped whereas var is operate scoped. 

Allow us to see an instance to know the distinction:

Instance:

Javascript

{

    let a = 2;

    var b = 4

    const c = "Good day"

    console.log(a)

    console.log(b)

    console.log(c)

}

  

console.log(b)

console.log(c)

console.log(a

Output:

 

Clarification: Since variables “a” and “c” are block scoped so we weren’t in a position to entry them exterior their block.

To study extra concerning the scope of variables consult with this text Understanding variable scopes in JavaScript

Comparability of properties of let, var, and const key phrases in JavaScript:

Property

var

let

const

Scope operate scoped block scoped block scoped
Updation mutable mutable immutable
Redeclarartion may be redeclared can’t be redeclared can’t be redeclared
Hoisting hoisted at high hoisted at high hoisted at high

RELATED ARTICLES

Most Popular

Recent Comments