Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

var vs let vs const

var: 
	- hoisted (always declared at top of scope, global if none)
    - function scope
let:
    - block scope
    - not redeclarable
const: 
    - block scope
    - not reassignable
    - not redeclarable
    
Note: Although it may seem like these hold only semantic meaning, using the
appropriate keywords helps the JS engines' compiler to decide on what to optimize.
Comment

difference between var and let

var is function scoped and let is block scoped. Let's say you have:
function understanding_var() {
	if (1 == 1) {
    	var x = 5;
        console.log('the value of x inside the if statement is ' + x);
    }
    console.log(x);
} 
//output: the value of x inside the if statement is 5
		  5

function understanding_let() {
	if (1 == 1) {
    	let x = 5;
        console.log('the value of x inside the if statement is ' + x);
    }
    console.log(x);
} 
//output: the value of x inside the if statement is 5
		  Uncaught ReferenceError: x is not defined
          
var is defined throughout the entire function, even if it's inside the if 
statement, but the scope of let is always within the curly braces, not outside
it, even if the conditional statement is inside the function.
Comment

let vs const

`const` is a signal that the identifier won't be reassigned.

`let` is a signal that the variable may be reassigned, such as a counter in a
loop, or a value swap in an algorithm.

It also signals that the variable will be used only in the block it's defined
in, which is not always the entire containing function.
Comment

what is the difference between let and const in javascript

The difference is that with const you can only only assign a value to a variable
once, but with let it allows you to reassign after it has been assigned.
Comment

difference between var, let, const

// var is a function scope ***
if(true){
    var varVariable = 'This is var';
    var varVariable = 'This is var again';
}

console.log(varVariable); // This is var again

// let is a block scope ***
if(true){
    let letVariable = 'This is let';
    let letVariable = 'This is let again';

    // let variable can't re-define but we can re-assign value


    console.log(letVariable); // let letVariable = 'This is let again';^SyntaxError: Identifier 'letVariable' has already been declared
}

console.log(letVariable); //ReferenceError: letVariable is not defined



// const variable can't re-define and re-assign value
// const is a block scope ***
if(true){
    const constVarible = {
        name: 'JavaScript',
        age: '25 years',
    };
    constVarible.name = 'JS';

    console.log(constVarible) // {name: 'JS',age: '25 years'} <= we can update const variable declared object 
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript multiple cases 
Javascript :: angular two way binding 
Javascript :: how to add debounce in react redux js 
Javascript :: check if specific letter exist in string javascript 
Javascript :: javascript match against array 
Javascript :: delete cookie 
Javascript :: mongodb find array with element 
Javascript :: javascript xhr set parameters 
Javascript :: set json column as index pandas dataframe 
Javascript :: disable livewire error model 
Javascript :: command reboot android app react native adb command 
Javascript :: how to download react table into pdf full 
Javascript :: javascript target closest class 
Javascript :: angular blockly 
Javascript :: opposite number js 
Javascript :: how to check url with port is valid or not regex javascript 
Javascript :: javascript const 
Javascript :: in in sequelize 
Javascript :: start nodemon under wsl2 
Javascript :: json schema example 
Javascript :: input mask 9 number add 
Javascript :: Javascript swap old and new method 
Javascript :: if operator ternary 
Javascript :: how to create an object in javascript 
Javascript :: sleep 1 second 
Javascript :: string js 
Javascript :: how to fetch data from json file in flutter 
Javascript :: how to not use relative imports in react js 
Javascript :: vue 3 $refs 
Javascript :: vue global computed property 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =