Ten JavaScript Questions

Ten JavaScript Questions

  1. Differences between keyword var and let

    • let was introduced in ES6, old browsers won’t support it, var has been around for a long time
    • let is block scoped while var is function scoped. When a var variable is declared in a block, you can still access it outside the block
    • let is not hoisted while var is hoisted
  2. Differences between ‘undefined’ and ‘null’

    • undefined means a variable is declared but never assigned to a value
    • null means a variable is declared and you assigned a value null to indicate that it is empty or it does not have anything.
    • typeof(undefined) === ‘undefined’ typeof(null) === ‘object’
  3. What is arrow function?

    • it is a ES6 feature.
    • it doesn’t create a new scope as normal functions do. Instead, it shares the same scope with its first outer function. It helps to avoid the issue with keyword ‘this’ not being the object that is intended.
  4. What is prototypal inheritance?

    • In JavaScript, all functions are objects. And they all have a property called ‘prototype’
    • You can add methods to ‘prototype’
    • when you create instance objects from constructor functions, those instance object will inherit the methods from constructor’s prototype