5 sposobów na ustawienie wartości domyślnej
let name = inputName || "John";
let name = inputName ?? "John";
let name = typeof inputAge !== "undefined" ? name : "John";
function createUser(name = "John", age = 18, isActive = true) {
return { name, age, isActive };
}
const config = { firstName: "Alice" };
const {
firstName = "John",
lastName = "Doe",
nickname = "N/A"
} = config;
const names = { firstName, lastName, nickname };
console.log(names)
Selektory rodzeństwa w CSS (Sibling Selectors)
h1 + p { color: red; }
.button + .button { margin-left: 10px; }
h1 ~ p { color: blue; }
.active ~ li { opacity: 0.5; }
p:has(+ h2) { margin-bottom: 0; }
li:has(+ .active) { border-right: 2px solid; }
div:has(img) + p { font-weight: bold; }
.item:has(.sold) ~ .item { filter: grayscale(1); }
3 sposoby na zapisanie danych w przeglądarce
localStorage.setItem("name", "John");
const fruits = ["apple", "banana", "pear"];
localStorage.setItem("fruits", JSON.stringify(fruits));
const user = { name: "Alice", age: 25 };
localStorage.setItem("user", JSON.stringify(user));
sessionStorage.setItem("settings", JSON.stringify({ theme: "dark", fontSize: 14 }));
document.cookie = "token=abc123; max-age=" + 7*24*60*60;
const colors = ["red", "green", "blue"];
document.cookie = `colors=${JSON.stringify(colors)}; max-age=${7*24*60*60}`;
const product = { id: 101, name: "Laptop", price: 2500 };
document.cookie = `product=${JSON.stringify(product)}; max-age=${7*24*60*60}`;