Answers for "let var and const"

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
2

difference in var let and const

var: 
	* hoisted(global scope)
    * function scope
let:
    * block scope
    * reassignable
    * not redeclarable
const: 
    * block scope
    * not reassignable
    * not redeclarable
Posted by: Guest on August-10-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
0

const let var

var makes real ice cubes that rattle around in New York
let makes real ice cubes in Manhatten 
const is like let but the ice cubes never change because they are plastic
Posted by: Guest on April-14-2021

Browse Popular Code Answers by Language