Answers for "var let and const in javascript"

7

difference between let, var and const in java script

//let is used to declare the variable or initialize a variable..
//let variable can be updated in future.
//Its block scope and we cannot redeclare it.

let a; // Initializing the variable.
let b = 4; // Initializing and declaring variable.
let b = 5; //Error:- cannot redeclare a value but we can update the value.

//var is just like let but var is used to declare a variable value at top of
//the program, its hoisted.

var a; // Initializing the variable.
var b = 4; // Initializing and declaring variable.
var b = 5; //b will be redeclared and we can update its value.

//const variable should be declared and intialized at same time.
//const variable cannot be updated in future.
//we cannot even redeclare the variable in future.

const a = 10; //Initilizing and declaring a variable at a time.
const b; //Error:- const should be declared and intialized.
const a = 11 // Error:- cannot redeclare the variable again.
Posted by: Guest on September-15-2021
13

let in javascript

The let statement declares a block scope local variable, optionally initializing it to a value.

let x = 1;

if (x === 1) {
  let x = 2;

  console.log(x);
  // expected output: 2
}

console.log(x);
// expected output: 1
Posted by: Guest on April-02-2020
0

difference between var let and const in javascript with example

//functional scope 
 var a; // declaration
 a=10; // initialization; 
//global scope
// re-initialization possible
 let a;//only blocked scope & re-initialization possible
 a=10;
let a =20;
if(true){
  let b =30;
}
console.log(b); // b is not defined
const // const also blocked scope,Re-initialization and re-declaration not possible
const a; // throws error {when we declaring the value we should assign the value.
const a =20;
if(true){
  const b =30;
}
console.log(b); // b is not defined
console.log(a); // no output here because code execution break at leve b.
Posted by: Guest on February-12-2021
2

var or const in javascript

// var declares a variable, meaning its value will vary. 
// const declares a constant, meaning its value will remain 
// consistant and not change. 
// If your variable changes throughout the program or website, 
// declare it using a var statement. 
// Otherwise, if its value does not change, declare it using 
// a const statement. 

const myConst='A const does not change.';

var myVar='A var does change.';

var myVar=2;
Posted by: Guest on July-09-2020
0

difference between var and let and const in javascript

var variable1 // declared using var
const variable2 // declared using const

myFunction1();

function myFunction1() {
  variable1 = "hello!";
  console.log(variable1);
  // "hello!"
}

myFunction2();

function myFunction2() {
  variable2 = "hello!";
  // error: variable2 is a constant and can not be redifined
}

myFunction3(true);

myFunction3(codition) {
  if(condition) {
    let variable3 = "helo!" // declared using const
  }
  variable3 = "hello!";
  // error: variable3 is out of scope
}

/*

Therefore, 
  
  var:
    -can be declared outside any function to be used inside any function
    -can be declared inside any function or any other {} which are of only if or if-else or switch etc. and can be used anywhere inside the function
    -can be changed again and again anywhere
  let:
    -can be declared outside any function to be used inside any function
    -if declared inside any function or any other {} which are of only if or if-else or switch etc. and can't be used anywhere inside the function and can be only used inside statement
    - can be changed again and again only inside the statement in which they are made in
    const:
    -can be declared outside any function to be used inside any function
    -can be declared inside any function or any other {} which are of only if or if-else or switch etc. and can be used anywhere inside the function
    -cannot be changed again and agan anywhere, if tried to, will result in an error

*/
Posted by: Guest on July-23-2021
0

const let var

var greeter;
    console.log(greeter); // greeter is undefined
    greeter = "say hello"
Posted by: Guest on March-30-2021

Browse Popular Code Answers by Language