const user = {
name: "Alice",
address: null,
preferences: {
theme: "dark"
}
};
const city = user.address?.city ?? "Unknown";
const theme = user.preferences?.theme ?? "light";
const { name, address } = user;
const numbers = [1, 2, 3, 4, 5];
const [first, second, ...rest] = numbers;
const double = x => x * 2;
const isEven = x => x % 2 === 0;
const sum = numbers
.filter(isEven)
.map(double)
.reduce((acc, x) => acc + x, 0);
class Shape {
constructor(name) {
this.name = name;
}
describe() {
return `This is a ${this.name}`;
}
}
class Circle extends Shape {
constructor(radius) {
super("circle");
this.radius = radius;
}
get area() {
return Math.PI * this.radius ** 2;
}
}
async function fetchUserData(userId) {
try {
const response = await fetch(`/api/users/${userId}`);
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error("Fetch failed:", error);
throw error;
}
}
const results = await Promise.all([
fetchUserData(1),
fetchUserData(2),
fetchUserData(3)
]);
export { Circle, fetchUserData };
import { Circle } from './shapes.js';