0
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 ?
6
  • Read about var, let and const in JavaScript. Commented Jul 3, 2018 at 10:29
  • You should refer MDN declarations Commented Jul 3, 2018 at 10:30
  • 1
    "Is there any logic/reason to define 's' as var or let in such a situation?" -- in the code you posted there is no difference between var, let and const. To see the differences you need to use them in larger functions. Commented Jul 3, 2018 at 10:30
  • @axiac: i read the MDN - and i understand that the const is the right here but behind this, is there some reason to define read only object as something else ? maybe to optimaiz the memory ? the const/let/var will collect by GC in same way ? Commented Jul 3, 2018 at 10:33
  • 1
    Read about function expressions and arrow functions. They are slightly different here and there but, in essence, they are functions. Commented Jul 3, 2018 at 10:33

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

-1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.