6 tricky JavaScript interview questions

6 tricky JavaScript interview questions

Interview questions can be tricky, and answering them right can help you land your next job. Practice makes perfect! Let's go through six questions you should know as a JavaScript developer.

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

Consider the following code.

console.log(5 === "5"); // false
console.log(5 == "5"); // true

Triple equals will check for type and value. When the number 5 is compared with the string 5 it will return false because they are different types.

Double equals do type coercion, meaning it will first convert the values to the same type before it makes the comparison. So when comparing strings with numbers it does not matter if they are different types, it will return true as long as the value is the same.

In most cases, it's recommended to stick with triple equals to avoid bugs.

What happens when you add two floats together? for example 0.1 + 0.2

let x = 0.1 + 0.2; 
let y = 0.3;
console.log(x === y); // false

What's going on here? we know that 0.1 + 0.2 is actually equal 0.3, so why is false printed? Let's print both variables to see the reason behind this.

console.log(x); // 0.30000000000000004
console.log(y); // 0.3

The problem is not really related to JavaScript, it's more of a general computer problem. Computers use binary floating point to do calculations and that cannot be represented in an accurate way.

What if need to handle currency?

One solution would be to represent it as an integer. For example, if our currency is dollar, we would store it in cent.

const dollar = 95.3;
const cent = 9530;

Explain the code below

const person1 = {
    age: 27,
    name: "Bob"
}

const person2 = person1;

person1.age = 10;

console.log(person2.age); // 10
console.log(person2.age) // 10

Both person1 and person2 point to the same object in memory, it means that if we change the property age to a new value all other variables referencing the same object will also be updated. Object is a reference type, so is Array and Function.

We also have something called primitive type, this include Boolean, null, undefined, String, and Number. Here is an example of how that works.

let sum = 13;
let newSum = sum;

sum = 25;

console.log(sum); // 25
console.log(newSum); // 13

When the sum is updated, newSum keeps its original value. In other words, we are not working with a reference here but the actual value.

How does asynchronous code work in javascript?

Javascript is a single threaded programming language, it can only execute on thing at the time. Imagine that javascript did everything synchronous that would mean we had to wait for a task to finish before we can continue with next ones.

An example would be if we send an API request, and then the user clicks on a button nothing would happen because we are still waiting for the API request to finish.

Instead, when we make an API request, JavaScript will tell us when we get a response and while we wait for the response we can do other stuff in the meantime, for example clicking a button.

Example of callback

function cleanRoom () {
    console.log("cleaning room");
}

function calLFriend() {
    console.log("calling friend");
}

function orderPizza(callback){
  // just to simulate api request / code delay
  setTimeout(function(){
      // Pizza is ready, can now execute the function passed in.
      callback();
  }, 2000 );
}

function eatPizza() {
    console.log("eating pizza nom nom");
}
 
 orderPizza(eatPizza);
 cleanRoom();
 callFriend();

// cleaning room
// calling friend
// eating pizza nom nom

We first create four functions pay attention to orderPizza. what happens inside the function is not so important, we can imagine we make an API call to Domino's Pizza and they will cook and deliver a pizza. What matter is it's accepting a function as an argument, this will be executed when the pizza is ready.

We call orderPizza and pass eatPizza as an argument. We don't know how long it will take for Domino's to cook and deliver the pizza, but we wanna be notified once the order is ready. While we wait for the pizza we execute two other functions cleanRoom and callFriend. After a short moment, we will see "eating pizza nom nom".

Imagine if this was synchronous we would have to wait for Domino's to deliver before we could clean the room and call our friend.

Other examples of asynchronous techniques in Javascript.

Promises
Async/await

What does "use strict" do?

"use strict" was added in ECMAScript 5, this allows javascript to run in a different mode that is more strict. It prevents silent errors and instead throws them. "use strict" is usually placed at the top of the javascript file, but can also be put inside a function then only that function will run in "strict mode".

Some examples where strict mode will throw errors

"use strict";

name = "Patrik";  // ReferenceError: name is not defined

It's not allowed to assign variable a value without declaring it.

"use strict";

function multi(a, a) {
    return a * a
};   

// syntaxError: Duplicate parameter name not allowed in this context

You can't have parameters with the same name.

"use strict";

var let = 10; 
// SyntaxError: Unexpected strict mode reserved word

You can't use reserved words as variables.

Should I always use "use strict"?

If you already have an existing code base you should be careful with implementing it. It can lead to new errors that you did not have before. For new projects, it's recommended to use it.

ES6 modules are always in strict mode

For es6 modules you don't have to write "use strict", it's already the default behavior.

What is the difference between undefined and null?

Variables that have been declared but not been assigned a value will be given the default value of undefined. Javascript will not set a value to null that will only be done programmatically.

If we write typeof this is the output we get.

console.log(typeof null); // "object"
console.log(typeof undefined); // "undefined"

surprised? Null is actually a primitive type but typeof returns "object" it's considered to be a bug in the language.