Top 5 JavaScript Interview Questions I Wish I Knew
Q1: Explain equality in JavaScript
JavaScript has both strict and type–converting comparisons:
- Strict comparison (e.g., ===) checks for value equality without allowing coercion
- Abstract comparison (e.g. ==) checks for value equality with coercion allowed
let a = '42';
let b = 42;
a == b; // true
a === b; // false
Some simple equalityrules:
If either value (aka side) in a comparison could be the true or false value, avoid == and use ===. If either value in a comparison could be of these specific values (0, "", or [] — empty array), avoid == and use ===. In all other cases, you’re safe to use ==. Not only is it safe, but in many cases it simplifies your code in a way that improves readability.
Q2: What does “use strict” do?
The use strict literal is entered at the top of a JavaScript program or at the top of a function and it helps you write safer JavaScript code by throwing an error if a global variable is created by mistake. For example, the following program will throw an error:
function foo(arg) {
"use strict";
x = arg + 10;
}`
It will throw an error because x was not defined and it is being set to some value in the global scope, which isn’t allowed with use strict The small change below fixes the error being thrown:
function foo(arg) {
'use strict';
var x = arg + 10;
}
Q3: Explain Null and Undefined in JavaScript
JavaScript (and by extension TypeScript) has two bottom types: null and undefined. They are intended to mean different things:
Something hasn’t been initialized: undefined. Something is currently unavailable: null.
Q4: Explain Values and Types in JavaScript
JavaScript has typed values, not typed variables. The following built-in types are available:
- string
- number
- boolean
- null and undefined
- object
- symbol (new to ES6)
Q5: What is let keyword in JavaScript?
In addition to creating declarations for variables at the function level, ES6 lets you declare variables to belong to individual blocks (pairs of { .. }), using the let keyword.