Basic
Type Conversion
{js title="To string"} value = String(true);
{js title="To number"} value = Number("123");
{js title="To int"} value = parseInt("123.0");
{js title="To float"} value = parseFloat("12.345");
{js title="To boolean"} value = Boolean("string");
Null Handling
{js title="Nullish coalescing operator"} alert(firstName ?? lastName ?? nickName ?? "Anonymous User");
{js title="Optional chaining (property)"} user?.address?.street
{js title="Optional chaining (function)"} user.admin?.();
{js title="Optional chaining (array)"} user?.[key]
Data Structure
Array
{js title="Add to beginning"} arr.unshift(...items);
{js title="Add to end"} arr.push(...items);
{js title="Extract from beginning"} let firstItem = arr.shift();
{js title="Extract from end"} let lastItem = arr.pop();
{js title="Find index of item"} arr.indexOf(item, pos);
{js title="Find last index of item"} arr.lastIndexOf(item, pos);
{js title="Check if includes value"} arr.includes(value);
{js title="Find first element"} arr.find(item => item.id === 1);
{js title="Filter elements"} arr.filter(item => item.status === 'active');
{js title="Find index of first element"} arr.findIndex(item => item.id === 1);
Promise
{js title="Create a new Promise"} let promise = new Promise((resolve, reject) => { /* async operation */ });
{js title="Handle Promise fulfillment"} promise.then(result => alert(result))
{js title="Handle Promise rejection"} promise.catch(error => alert(error));
{js title="Execute code after Promise settles"} promise.finally(() => console.log('Promise finished, regardless of outcome.'));
{js title="Wait for all Promises to fulfill"} Promise.all([promise1, promise2]).then(results => console.log(results));
{js title="Wait for all Promises to settle"} Promise.allSettled([promise1, promise2]).then(results => console.log(results));
{js title="Wait for the first Promise to settle"} Promise.race([promise1, promise2]).then(firstResult => console.log(firstResult));
{js title="Create an already resolved Promise"} let resolvedPromise = Promise.resolve("Success!");
`{js title=“Create an already rejected Promise”} let rejectedPromise = Promise.reject(new Error(“Failed!”));“
new Promise(resolve => setTimeout(() => resolve(1), 1000))
.then(result => {
console.log(result); // 1
return result * 2;
})
.then(result => {
console.log(result); // 2
return result * 2;
})
.then(result => {
console.log(result); // 4
});Module
export function sayHi(user) {
alert(`Hello, ${user}!`);
}import {sayHi} from './sayHi.js';
sayHi('John');Fetch
fetch(url)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));let promise = fetch(url, {
method: "GET", // POST, PUT, DELETE, etc.
headers: {
"Content-Type": "text/plain;charset=UTF-8"
},
body: undefined // string, FormData, Blob, BufferSource, or URLSearchParams
referrer: "about:client", // or "" to send no Referer header,
referrerPolicy: "no-referrer-when-downgrade", // no-referrer, origin, same-origin...
mode: "cors", // same-origin, no-cors
credentials: "same-origin", // omit, include
cache: "default", // no-store, reload, no-cache, force-cache, or only-if-cached
redirect: "follow", // manual, error
});