Wednesday, March 8, 2023
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 might be something from Quantity, Boolean, String, and even a worth returned from a operate. The values of the variables are allotted utilizing the task operator(“=”). 

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

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

JavaScript is a dynamically typed language so the kind of variables is set at runtime. Subsequently there isn’t a must explicitly outline the kind of a variable. We are able to 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 essential job 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 be taught extra about JavaScript let examine 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 might be modified however variables declared utilizing const are immutable. 

To be taught extra about JavaScript const examine 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 grasp 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 capable of entry them outdoors their block.

To be taught extra concerning the scope of variables confer 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 might be redeclared can’t be redeclared can’t be redeclared
Hoisting hoisted at high hoisted at high hoisted at high

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto