K I R A

Loading

Kira Intrilogy - Information & Technology HTML Template
K I R A

Loading

Blog Details

let var const

var vs let vs const

28 August 2022 by Kira Intrilogy

You can declare variables in JavaScript in 3 ways, by utilising the const, let, and var keyword. Each type of variable behaves differently during the processes of declaration, initialization, value access, and assignment. Lets brief into the difference and scopes. You can strengthen your understanding of JavaScript variables by reading this article, so let's get started.

var

Declaration

This variables can easily declare multiple times. If it is declared, they can also reassign there values.

var message = "Hey"
var message = "Hey Dear"
console.log(message)// "Hey Dear"
Scope

In var variables they have a global scope so that one can are allow access from the script anywhere. To a function block, though, they can be scoped.

var i = 2
if (i < 0) {
   var message = "Hey"
}
console.log(message) // "Hey"

As of right now, the get Message function is the only place where the message variable may be accessed

//This will be global scope
var greeting = "Hey Dear" // This will be function scope
function getMessage () {
   var message = "Hey"
   return message
}

console.log(message) //Uncaught ReferenceError: message is not defined

One characteristic of the var variable is that it becomes a property in the global window object once it is declared in the global scope (for example, at the very beginning of the script).

var message = "Hey"
console.log(window.message) //"Hey"

let

Declaration

This variables can declared only once. If it is declared more than once then a Syntax Error dropped onto the console but can reassign a value.

let message = "Hey"
let message = "Hey Dear"
//Uncaught SyntaxError: Identifier 'message' has already been declared
Scope

A Block of code is a section of code bordered by {}.Curly braces stays on a block and anything which in curly braces are block. As a result, a variable declared in a block with let can only be used in that block.

let i = 2
if (i > 0) {
   let message = "Hey"
   console.log("message variable inside if block: ", message)
}
console.log("message variable outside if block: ", message)
// Output:// "message variable inside if block: Hey"
// Uncaught ReferenceError: message is not defined

const

Declaration

As like let, this variable cannot declared more than one as a Syntax Error dropped onto the console and also this variable cannot reassign a value

const message = "Hey"
const message = "Hey"
// Uncaught SyntaxError: Identifier 'message' has already been declared
Scope

const is a block-scoped variable, just like the let variable. As a result, it can only be reached from within a single block, which is identified by a series of curly brackets.

const i = 2
if (i > 0) {
   const message = "Hey"
   console.log("message variable inside if block: ", message)
}
console.log("message variable outside if block: ", message)
// Output:
// "message variable inside if block: Hey"
// Uncaught ReferenceError: message is not defined

About Me

Rosalina D. Willaimson

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore.

Search Objects

Never Miss News

Instagram Feeds

Ready To Get Free Consulations For
Any Kind Of It Solutions ?