Overview
Navigating through JavaScript as a beginner can often lead to confusion, especially when certain concepts sound strikingly similar. In interviews, it's not uncommon to mistake one for another. Let's revisit these concepts to ensure clarity and understanding.
Nullish Coalescing Operator with Logical OR (||) and Logical AND (&&)
Nullish Coalescing
const name = "";
const defaultName = name ?? "Unknown";
console.log(defaultName); // Output: "''" (empty string)
The nullish coalescing operator (??
) is used to provide a default value if a variable is null
or undefined
, but not for other falsy values like 0
or ''. So if you used ||
for checking 0 or string it's gonna give you false always so used ??
instead
Logical OR (||) and Logical AND (&&):
const age = 25;
const hasLicense = true;
// Using logical OR
const canDrive = age >= 16 || hasLicense;
console.log(canDrive); // Output: true
// Using logical AND
const canVote = age >= 18 && hasLicense;
console.log(canVote); // Output: true
The logical OR (||
) and logical AND (&&)
operators are used to perform boolean logic operations. The OR operator returns true
if at least one of the operands is true
, while the AND operator returns true
only if both operands are true
. Sometimes you mess up with like which side will be true
or false
but just think clearly left first and then go for right and then combine both condition for the result 😕
Type Coercion and Template literals
Type Coercion
Implicit vs. Explicit Coercion:
Beginners may not be aware that type coercion can occur implicitly (automatically) or explicitly (manually). Implicit coercion happens automatically when JavaScript expects one type but receives another, while explicit coercion involves using built-in functions like Number(), String(), or Boolean() to convert values to specific types.Loose vs. Strict Equality: JavaScript has
both loose equality (==) and strict equality (===) operators. Beginners may not understand the difference between them and inadvertently use loose equality for comparison, leading to unexpected results due to type coercion. Strict equality (===) checks both value and type, while loose equality (==) performs type coercion before comparison.Truthy and Falsy Values: JavaScript treats
certain values as either "truthy" or "falsy" in conditional contexts. Beginners may find it confusing that non-boolean values like numbers, strings, objects, and even functions can be evaluated as either true or false depending on their coercion context. For example, an empty string ("") coerces to false, while a non-empty string coerces to true.Implicit Conversion During Arithmetic
Operations: During arithmetic operations, JavaScript automatically converts operands to a common type before performing the operation. This can lead to unexpected results if beginners are not aware of how different types are coerced in mathematical expressions. For example, adding a string and a number may result in concatenation rather than addition.Coercion in Comparison Operators:
Comparison operators (<, >, <=, >=) also involve type coercion when comparing values of different types. Beginners may find it confusing that JavaScript converts values to a common type before performing comparisons, potentially leading to unexpected outcomes.
Template literals
const name = "John";
console.log(`Hello, ${name}!`); // Output: "Hello, John!"
Template literals provide an easy way to interpolate variables and expressions into strings. You maybe used this syntax a lot but on the interview you often forget about this name 😆
Default Parameter Values, Spread Operator, and Rest Operator
Default Parameter Values
function greet(name = "World") {
console.log(`Hello, ${name}!`);
}
greet(); // Output: "Hello, World!"
greet("John"); // Output: "Hello, John!"
Default parameter values allow you to specify default values for function parameters.
Rest Operator:
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3, 4, 5)); // Output: 15
The rest parameter syntax allows us to represent an indefinite number of arguments as an array. Sometimes you mixed up this with Spread Operator.
Spread Operator
const numbers = [1, 2, 3, 4, 5];
console.log(...numbers); // Output: 1 2 3 4 5
The spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected.
Optional Chaining and Destructuring Assignments
Destructuring Assignments:
// Destructuring an Object
const person = { name: "John", age: 30 };
const { name, age } = person;
console.log(name, age); // Output: "John 30"
// Destructuring an array
const colors = ["red", "green", "blue"];
// Extracting values from the array into separate variables
const [firstColor, secondColor, thirdColor] = colors;
console.log(firstColor); // Output: "red"
console.log(secondColor); // Output: "green"
console.log(thirdColor); // Output: "blue"
Destructuring assignments allow you to extract values from objects or arrays into distinct variables. Sometimes you confused about it's gonna only work for object. But not It's also gonna work for array.
Optional Chaining
const person = {
name: "John",
address: {
city: "New York",
},
};
console.log(person.address?.city); // Output: "New York"
Optional chaining allows you to safely access nested properties of an object without causing an error if a property is null
or undefined
. You used it often but forget about it's name.
Callbacks, promises (ES6), and async/await (ES7)
Callbacks:
Callbacks are functions passed as arguments to other functions, which are then invoked at a later time, usually after an asynchronous operation completes.
Confusion Points:
Callback Hell: When dealing with multiple nested callbacks, also known as "callback hell," code can become difficult to read and maintain due to the pyramid-like structure.
Error Handling: Error handling with callbacks can be cumbersome, especially when dealing with multiple asynchronous operations, as errors must be passed through each callback in the chain.
Scope and Context: Beginners may struggle with understanding scope and context within callback functions, leading to unexpected behavior or variable scoping issues.
Promises:
Promises are objects representing the eventual completion or failure of an asynchronous operation. They provide a cleaner alternative to callbacks for handling asynchronous code.
Confusion Points:
Promise Chaining: Chaining multiple promises using .then() can sometimes be confusing, especially when handling errors or passing data between promises.
Error Handling: Beginners may find error handling with promises challenging, particularly understanding when and where to use .catch() to handle errors in promise chains.
Understanding Promise States: Novices may struggle to understand the three states of a promise: pending, fulfilled, and rejected, and how to transition between them.
Async/Await (ES7):
Async/await is a modern JavaScript feature that allows for writing asynchronous code in a synchronous-like manner. It is built on top of promises and provides a more intuitive syntax for handling asynchronous operations.
Confusion Points:
Syntax: While async/await offers a more straightforward syntax compared to callbacks and promises, beginners may still find the syntax confusing, especially when using async functions and
await
expressions.Error Handling: Understanding how to handle errors with async/await, including using try/catch blocks to catch and handle exceptions, can be challenging for newcomers.
Promise vs. Await: Distinguishing between returning a promise and using await within an async function can sometimes be confusing, leading to errors or unexpected behavior.
Top comments (0)