logo
shape

JavaScript

JavaScript can manipulate the content of the web page without the need for reloading the entire page. You may hear JavaScript often referred to as “Vanilla JavaScript.”

JavaScript - MDN Web Docs Glossary
Use JavaScript Short-circuits in Logical Operations to avoid checking if an object is null or undefined before accessing its properties.

let obj = null;
let key = obj && obj.key;  // This will not throw an error, key will be set to null
            
In JavaScript, you can assign default values to function parameters.

function greet(name = "Nasir") {
console.log("Hello, " + name + "!");
}
greet();  // Outputs: "Hello, Nasir!"
            
Destructuring assignment allows you to unpack values from arrays or properties from objects into distinct variables

let [a, b] = [1, 2];
console.log(a); // Outputs: 1
console.log(b); // Outputs: 2
            
The spread operator can be used to spread elements of an iterable (like an array or string) into places where zero or more arguments or elements are expected.

let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5];
console.log(arr2); // Outputs: [1, 2, 3, 4, 5]
            
Most Asked Questions about JavaScript
What is hoisting in JavaScript?

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing scope during the compile phase, before the code has been executed. It's important to note that only the declarations are hoisted, not initializations. If a variable is declared and initialized after using it, the variable will be undefined. For example,

console.log(myVar); // Output: undefined
var myVar = 5;
console.log(myVar); // Output: 5

Here, myVar declaration is hoisted to the top, but its initialization with the value of 5 is not, leading to undefined when first logged. Learn more about hoisting in this book.

What is the difference between null and undefined in JavaScript?

In JavaScript, undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value that represents no value or no object. It implies an empty or non-existent value. It's important to note that JavaScript treats undefined and null as two distinct types: typeof undefined returns undefined, whereas typeof null returns object. Learn more about JavaScript terminology in this book.

What are JavaScript Promises and how do they work?

A Promise in JavaScript is an object representing the eventual completion or failure of an asynchronous operation. It serves as a link between the function executing the asynchronous operation (the promise "producer") and the functions which will handle the result of that operation (the promise "consumers").

The Promise object can be in one of three states: pending (the initial state), fulfilled (operation completed successfully), or rejected (operation failed). Promises are used to handle asynchronous operations, and they provide a more flexible and powerful way of handling asynchronous code than traditional callback functions, reducing the risk of ending up in "callback hell". Learn more about JavaScript Promises and "callback hell" in this book.

© All rights Reserved. 2023