JavaScript Flashcards

1
Q

Explain the difference between var, let, and const and when you’d use each.

A
  • all ways to declare variables in JS
  • var is the older way, use in cases of older browsers that might not support let/const
  • let is used when the variable data will change/be updated (like the total purchase value)
  • const is used to declare a variable that will remain constant (like a name of a person), and not reassigned.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the difference between null and undefined and how you’d use each.

A

Undefined: a variable has not been assigned a value, or not declared at all
Null: is an assignment value. It can be assigned to a variable as a representation of no value/used as a placeholder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between == and === ?

A

Comparison operator ==> a == b means a equals b. You can use == operator in order to compare the identity of two operands even though, they are not of a similar type.

Strict equality comparison operator ==> a === b means a only equals b if the types match. If the variable values are of different types, then the values are considered unequal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a “closure” in JavaScript and how is it useful?

A

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
To use a closure, define a function inside another function and expose it. To expose a function, return it or pass it to another function.
The inner function will have access to the variables in the outer function scope, even after the outer function has returned.

One common use: closures are used to give objects data privacy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly