Yanshof
Yanshof

Reputation: 9926

When to use const or var or let and lambda expresion internals

groups= [];

getGroup(id){
    const s = groups.find(t => t.id === id);
    return s;
}

getGroup(id){
    var s = groups.find(t => t.id === id);
    return s;
}


getGroup(id){
    let s = groups.find(t => t.id === id);
    return s;
}
  1. Is there any logic/reason to define s with var or let in such a situation?
  2. What does js do in background of the code in lambda expression? Does it keep the values of the group in some data struct that can handle this fast and avoid some forEach loop ?

Upvotes: 0

Views: 2631

Answers (3)

Om Pandey
Om Pandey

Reputation: 19

let and const key are same only differencs are

let key words is block scoped it can access inside the block and same const as wll we cannot change the value directly of const variable but we can change the reference of object.

Upvotes: -1

LordTribual
LordTribual

Reputation: 4249

var is scoped to the nearest function block, whereas let is scoped to the nearest enclosing block.

Let's assume you have a function, and inside that function there's a for-loop. If you define and declare the iterator using var you could access it outside the function:

function foo() {  
  for(var i = 0; i <= 5; i++) {
    console.log(i);
  }

  // i is 6
  console.log(i);
} 

If you use let instead, i will be scoped only to the nearest enclosing block which is the for-loop:

function foo() {  
  for(let i = 0; i <= 5; i++) {
    console.log(i);
  }

  // i is undefined
  console.log(i);
}

But as a general rule, I almost never use var over let at least when I am using ES2015. That said, I cannot really think of a case where var would make sense and let not. Also, if you need to re-assign a variable, go with let and if the variable is never re-assigned use const.

Keep in mind tho that const does only mean immutabilty for primitive values and not for objects, meaning that you can still change property on an object even though you used const. It only guards from re-assigning the variable.

Personally, in most cases, I use const.

Upvotes: 3

nitishagar
nitishagar

Reputation: 9413

There are multiple good resources which provide more details on this [link1, link2]. Summary below:

  • let: Block scope and initialization is optional.
  • const: Block scope and initialization is required.
  • var: Assignment is optional and its not block scoped.

Relating to your lambda expression - these are arrow functions in javascript and provide shorter syntax for function expression.


Now given some context, answers inline to your queries:

  • Is there any logic/reason to define s with var or let in such a situation?
    • var is much more relaxed in terms of constrain as described above, so any variable in global scope with same identifier will be affected. let keeps this block scoped.
  • What does js do in background of the code in lambda expression? Does it keep the values of the group in some data struct that can handle this fast and avoid some forEach loop ?
    • The above lambda expression is just function declaration. The find function here takes the arrow function to perform the boolean check.

Upvotes: 1

Related Questions