What are Modules?
In JavaScript, modules allow you to split your code into smaller chunks. You can export pieces of your code (like functions, variables) from one file and import them in other files. This makes your code cleaner and easier to maintain. π
Example:
module.js:
export const greeting = "Hello, World!";
export function greet(name) {
return `Hello, ${name}! π`;
}
main.js:
import { greeting, greet } from './module.js';
console.log(greeting); // "Hello, World!"
console.log(greet('Alice')); // "Hello, Alice! π"
?.
) π€What is Optional Chaining?
Optional chaining is like knocking on a door to check if someone is home. If the door doesnβt exist, you donβt crash into it, you simply move on. π
With ?.
, if a property doesnβt exist, it wonβt throw an error; itβll return undefined
instead.
Example:
const user = { name: "Alice", address: { city: "Wonderland" } };
console.log(user.address?.city); // "Wonderland" ποΈ
console.log(user.profile?.bio); // undefined β