DEV Community

jiiin✨
jiiin✨

Posted on

JS basics - 1

This questions if from: 70 JavaScript Interview Questions

  1. Compre undefined vs. null (my initial answer) Undefined and Null both are primitive value in Javascript, but I normally use Undefined when a variable is supposed to have a value in future but not sure which value it will be. Meanwhile I use null to identify the variable should not contain any reference.

(correct answer)
JavaScript's 7 primitive types.
string, number, null, undefined, boolean, symbol, bigint, object

Both are falsy values by converting Boolean(value) object or double Not operator (!!value).

undefined:

  • Default value of a variable that has not been assigned a specific value yet
  • A function that has no explicit return value
  • Property that does not exist in an object

null:

  • A value that explicitly represents no value

null == undefined // abstract equality, compares value only, true
null === undefined // strict equality, compares value and type, false

  1. What does the && operator do? Logical And operator compares conditions and returns true if all conditions are true.
const trueResult = true && true // true
const falsyResult = true && false // false
Enter fullscreen mode Exit fullscreen mode

Logical operator returns true if first operand is true,
or returns right operand value if left is false

Logical And operator evaluates from left to right operands, and returns first falsy value*. If none is false, returns last expression.

const value = " " && true && 'apple' // 'apple'
const apple = false && 'apple' && 'orange' // false
Enter fullscreen mode Exit fullscreen mode

Falsy value:

  • undefined
  • null
  • NaN
  • empty string
  • false
  • 0
  1. What does Logical Or operator do? Logical Or operator evaluates if any conditions are true, then return true. If one of any conditions are false, returns false

For example

x || y
Enter fullscreen mode Exit fullscreen mode

If x can be converted true, return x. Else, return y.

[some truthy expression] || expr
Enter fullscreen mode Exit fullscreen mode

Top comments (0)