Declaration
Code
Notes
var
var a = 1;
Can be updated and re-declared.
let
let a = 1;
Can be updated but cannot be re-declared.
const
const a = 1;
Cannot be updated or re-declared.
🔼Back to Top
newArray.forEach((item) => {
console.log(item);
});
2.5. Loop through Array with Index
newArray.forEach((item, index) => {
console.log(index, item);
});
2.6. Loop through Array in Reverse
newArray.reverse().forEach((item) => {
console.log(item);
});
🔼Back to Top
for (const key in newObject) {
console.log(key, newObject[key]);
}
3.5. Loop through Object with Index
Object.entries(newObject).forEach(([key, value]) => {
console.log(key, value);
});
3.6. Loop through Object in Reverse
Object.entries(newObject).reverse().forEach(([key, value]) => {
console.log(key, value);
});
🔼Back to Top
function newFunction() {
console.log('newFunction');
}
4.3. Call Function with Parameter
function newFunctionWithParameter(parameter) {
console.log(parameter);
}
4.4. Call Function with Parameter and Return Value
function newFunctionWithParameterAndReturnValue(parameter) {
return parameter;
}
🔼Back to Top
class NewClass {
constructor() {
console.log('NewClass');
}
}
5.2. New Class with Constructor
class NewClassWithConstructor {
constructor(parameter) {
console.log(parameter);
}
}
5.3. New Class with Constructor and Method
class NewClassWithConstructorAndMethod {
constructor(parameter) {
console.log(parameter);
}
method() {
console.log('method');
}
}
5.4. New Class with Constructor and Method and Property and Getter
class NewClassWithConstructorAndMethodAndPropertyAndGetter {
constructor(parameter) {
console.log(parameter);
}
method() {
console.log('method');
}
property = 'property';
get getter() {
return 'getter';
}
}
5.5. New Class with Constructor and Methods and Inheritance
class NewClassWithConstructorAndMethodsAndInheritance extends NewClassWithConstructorAndMethod {
constructor(parameter) {
super(parameter);
}
method() {
super.method();
console.log('method');
}
}
🔼Back to Top
if (true) {
console.log('true');
}
if (true) {
console.log('true');
} else {
console.log('false');
}
if (true) {
console.log('true');
} else if (false) {
console.log('false');
}
switch (true) {
case true:
console.log('true');
break;
case false:
console.log('false');
break;
}
🔼Back to Top
for (let i = 0; i < 10; i++) {
console.log(i);
}
const newObject = {
key1: 'value1',
key2: 'value2',
key3: 'value3',
};
for (const key in newObject) {
console.log(key, newObject[key]);
}
const newArray = [1, 2, 3];
for (const item of newArray) {
console.log(item);
}
let i = 0;
while (i < 10) {
console.log(i);
i++;
}
let i = 0;
do {
console.log(i);
i++;
} while (i < 10);
🔼Back to Top
try {
console.log('try');
} catch (error) {
console.log('catch');
}
try {
console.log('try');
} catch (error) {
console.log('catch');
} finally {
console.log('finally');
}
🔼Back to Top
const newPromise = new Promise((resolve, reject) => {
resolve('resolve');
});
9.2. New Promise with Resolve
const newPromiseWithResolve = new Promise((resolve, reject) => {
resolve('resolve');
});
9.3. New Promise with Reject
const newPromiseWithReject = new Promise((resolve, reject) => {
reject('reject');
});
9.4. New Promise with Resolve and Reject
const newPromiseWithResolveAndReject = new Promise((resolve, reject) => {
resolve('resolve');
reject('reject');
});
9.5. New Promise with Resolve and Reject and Async
const newPromiseWithResolveAndRejectAndAsync = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('resolve');
reject('reject');
}, 1000);
});
🔼Back to Top
async function newAsyncFunction() {
console.log('newAsyncFunction');
}
10.2. New Async Function with Await
async function newAsyncFunctionWithAwait() {
await newPromiseWithResolveAndRejectAndAsyncAndAwait;
console.log('newAsyncFunctionWithAwait');
}
🔼Back to Top
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then((response) => response.json())
.then((json) => console.log(json));
const newFetchWithUrl = fetch('https://jsonplaceholder.typicode.com/todos/1');
11.3. New Fetch with URL and Method
const newFetchWithUrlAndMethod = fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
});
11.4. New Fetch with URL and Method and Headers
const newFetchWithUrlAndMethodAndHeaders = fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
11.5. New Fetch with URL and Method and Headers and Body
const newFetchWithUrlAndMethodAndHeadersAndBody = fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
});
11.6. New Fetch with URL and Method and Headers and Body and Async
const newFetchWithUrlAndMethodAndHeadersAndBodyAndAsync = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
});
const json = await response.json();
console.log(json);
};
11.7. New Fetch with URL and Method and Headers and Body and Async and Await
const newFetchWithUrlAndMethodAndHeadersAndBodyAndAsyncAndAwait = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
});
const json = await response.json();
console.log(json);
};
11.8. New Fetch with URL and Method and Headers and Body and Async and Await and Response
const newFetchWithUrlAndMethodAndHeadersAndBodyAndAsyncAndAwaitAndResponse = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
});
const json = await response.json();
console.log(json);
};
11.9. New Fetch with URL and Method and Headers and Body and Async and Await and Response and Json
const newFetchWithUrlAndMethodAndHeadersAndBodyAndAsyncAndAwaitAndResponseAndJson = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
});
const json = await response.json();
console.log(json);
};
🔼Back to Top
const newEvent = new Event('build');
12.2. New Event with Type
const newEventWithType = new Event('build');
12.3. New Event with Type and Options
const newEventWithTypeAndOptions = new Event('build', {
bubbles: true,
cancelable: true,
});