layout | title |
---|---|
../../layouts/CheatSheet.astro |
Javascript Cheatsheet |
Javascript is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions. While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as Node.js, Apache CouchDB and Adobe Acrobat. JavaScript is a prototype-based, multi-paradigm, single-threaded, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles.
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. |
const newArray = [];
newArray.push(1); //To the end of array
newArray.unshift(1); //To the beginning of array
newArray.pop(); //Last element of array
newArray.shift(); //First element of array and return the value of removed element
newArray.forEach((item) => {
console.log(item);
});
newArray.forEach((item, index) => {
console.log(index, item);
});
newArray.reverse().forEach((item) => {
console.log(item);
});
const newObject = {};
newObject.key = 'value';
delete newObject.key;
for (const key in newObject) {
console.log(key, newObject[key]);
}
Object.entries(newObject).forEach(([key, value]) => {
console.log(key, value);
});
Object.entries(newObject).reverse().forEach(([key, value]) => {
console.log(key, value);
});
function newFunction() {
console.log('newFunction');
}
newFunction();
function newFunctionWithParameter(parameter) {
console.log(parameter);
}
function newFunctionWithParameterAndReturnValue(parameter) {
return parameter;
}
With the bind() method, an object can borrow a method from another object.
const person = {
firstName:"John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
const member = {
firstName:"Hege",
lastName: "Nilsen",
}
let fullName = person.fullName.bind(member);
class NewClass {
constructor() {
console.log('NewClass');
}
}
class NewClassWithConstructor {
constructor(parameter) {
console.log(parameter);
}
}
class NewClassWithConstructorAndMethod {
constructor(parameter) {
console.log(parameter);
}
method() {
console.log('method');
}
}
class NewClassWithConstructorAndMethodAndPropertyAndGetter {
constructor(parameter) {
console.log(parameter);
}
method() {
console.log('method');
}
property = 'property';
get getter() {
return 'getter';
}
}
class NewClassWithConstructorAndMethodsAndInheritance extends NewClassWithConstructorAndMethod {
constructor(parameter) {
super(parameter);
}
method() {
super.method();
console.log('method');
}
}
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;
}
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);
try {
console.log('try');
} catch (error) {
console.log('catch');
}
try {
console.log('try');
} catch (error) {
console.log('catch');
} finally {
console.log('finally');
}
const newPromise = new Promise((resolve, reject) => {
resolve('resolve');
});
const newPromiseWithResolve = new Promise((resolve, reject) => {
resolve('resolve');
});
const newPromiseWithReject = new Promise((resolve, reject) => {
reject('reject');
});
const newPromiseWithResolveAndReject = new Promise((resolve, reject) => {
resolve('resolve');
reject('reject');
});
const newPromiseWithResolveAndRejectAndAsync = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('resolve');
reject('reject');
}, 1000);
});
async function newAsyncFunction() {
console.log('newAsyncFunction');
}
async function newAsyncFunctionWithAwait() {
await newPromiseWithResolveAndRejectAndAsyncAndAwait;
console.log('newAsyncFunctionWithAwait');
}
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');
const newFetchWithUrlAndMethod = fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
});
const newFetchWithUrlAndMethodAndHeaders = fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
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,
}),
});
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);
};
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);
};
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);
};
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);
};
const newEvent = new Event('build');
const newEventWithType = new Event('build');
const newEventWithTypeAndOptions = new Event('build', {
bubbles: true,
cancelable: true,
});