DEV Community

Cover image for 70 JavaScript Interview Questions
Mark Abeto
Mark Abeto

Posted on • Updated on

 

JavaScript Interview Questions and Answers 70 JavaScript Interview Questions

Hi Guys Good Day and a Happy New Year 🎆🎆🎆!

This is a long one, so bear with me for a second or an hour. In every answer for every question there's an arrow up ↑ link that lets you go back to the List of Questions so that you don't waste time scrolling up and down.

The Questions

1. What's the difference between undefined and null?.

↑ Before understanding the differences between undefined and null we must understand the similarities between them.

  • They belong to JavaScript's 7 primitive types.
 let primitiveTypes = ['string','number','null','undefined','boolean','symbol', 'bigint'];
Enter fullscreen mode Exit fullscreen mode
  • They are falsy values. Values that evaluated to false when converting it to boolean using Boolean(value) or !!value.
   console.log(!!null); //logs false
   console.log(!!undefined); //logs false

   console.log(Boolean(null)); //logs false
   console.log(Boolean(undefined)); //logs false
Enter fullscreen mode Exit fullscreen mode

Ok, let's talk about the differences.

  • undefined is the default value of a variable that has not been assigned a specific value. Or a function that has no explicit return value ex. console.log(1). Or a property that does not exist in an object. The JavaScript engine does this for us the assigning of undefined value.
  let _thisIsUndefined;
  const doNothing = () => {};
  const someObj = {
    a : "ay",
    b : "bee",
    c : "si"
  };

  console.log(_thisIsUndefined); //logs undefined
  console.log(doNothing()); //logs undefined
  console.log(someObj["d"]); //logs undefined
Enter fullscreen mode Exit fullscreen mode
  • null is "a value that represents no value". null is value that has been explicitly defined to a variable. In this example we get a value of null when the fs.readFile method does not throw an error.
  fs.readFile('path/to/file', (e,data) => {
     console.log(e); //it logs null when no error occurred
     if(e){
       console.log(e);
     }
     console.log(data);
   });
Enter fullscreen mode