JavaScript
From Toptal.com
Questions and answers in this section are from Toptal.
What is the difference between the concepts of null and undefined in JavaScript? And how do you handle them?
Null represents an intentional absence of any object value, while undefined represents a variable that has been declared but has not been assigned a value.
If a developer doesn’t need to know whether the variable is null or undefined, they can use a standard equality check against null; if they do, they’ll need a strict equality check.
A good follow-up question: Ask the candidate to explain a time when they faced a situation needing a null check and the reasoning behind whether it needed to be strict or not.What is the difference between the equality (==) and strict equality (===) operator? When should you use which?
The equality operator performs type coercion, which means it attempts to convert the operands to a common type before making the comparison. The strict equality operator never converts before comparing.
The equality operator is quite dangerous to use and is generally frowned upon since it can yield unexpected results, like the fact that an empty string is considered equal to the number zero when the two are coerced to the same type.
What are the differences between let, const, and var in JavaScript?
var is function-scoped and can be redeclared and reassigned.
let and const are block-scoped, and let allows reassignment, while const does not.
It can also be fruitful to discuss a candidate’s choice of various tools and libraries for your upcoming JavaScript use cases.
Arrays
How can you calculate a total from an Array?
const total = [1,2,3].reduce((total, val) => val + total, 0);
console.log(total); // 6
Hoisting
Key aspects of JavaScript hoisting
Hoisting is JavaScript's default behavior of moving declarations to the top of the current scope (to the top of the current script or the current function). The JavaScript code is not changed but the declarations are collected and tracked in lexical environment.
A variable can be used before it has been declared.
A var declaration is hoisted and initialized with a value of undefined if the var is not defined with a value.
Variables defined with let and const are hoisted to the top of the block, but not initialized.
Using a let variable before it is declared will result in a ReferenceError.
Using a const variable before it is declared, is a syntax error, so the code will simply not run.
Object
Deep clone an Object
- You can use the
structuredClone()function on the Window object to perform a deep clone. - Object.assign only copies property values. Any object references remain the same so changing values within an object reference would carry over to all objects that hold that reference.
// Deep Clone
const obj3 = { a: 0, b: { c: 0 } };
const obj4 = structuredClone(obj3);
obj3.a = 4;
obj3.b.c = 4;
console.log(obj4); // { a: 0, b: { c: 0 } }
How can you get the count of properties/keys in an Ojbect?
const propCount = Object.keys(someObj).length;