Javascript 3
Javascript 3
@codingtute
JS
console.log(a) // prints 3 const entries = Object.entries(obj);
/* returns an array of [key, value]
Promises with finally
Const pairs of the object passed */
promise
// can be assigned only once console.log(entries);
.then((result) => { ··· })
const a = 55 /* prints
.catch((error) => { ··· })
a = 44 // throws an error [
.finally(() => { /* logic
['firstName', 'FirstName'],
independent of success/error */ })
Multiline string ['lastName', 'LastName'],
/* The handler is called when the
console.log(` ['age', 24],
promise is fulflled or rejected.*/
This is a ['country', 'India']
multiline string ]; */
`)
String includes()
/* {firstName: "FirstName", lastName: "LastName2",
console.log('apple'.includes('pl')) canSing: true} */
// prints true /* great for modifying objects without side
console.log('apple'.includes('tt')) effects/affecting the original */
// prints false
String startsWith()
console.log('apple'.startsWith('ap')) Destructuring Nested Objects
//prints true const Person = {
console.log('apple'.startsWith('bb')) name: "Harry Potter",
//prints false age: 29,
sex: "male",
String repeat() materialStatus: "single",
console.log('ab'.repeat(3)) address: {
//prints "ababab"
country: "USA",
state: "Nevada",
Destructuring array
city: "Carson City",
let [a, b] = [3, 7];
console.log(a); // 3 pinCode: "500014",
console.log(b); // 7 },
};
Destructuring object const { address : { state, pinCode }, name } = Person;
let obj = { console.log(name, state, pinCode)
a: 55, // Harry Potter Nevada 500014
b: 44 console.log(city) // ReferenceError
};
let { a, b } = obj;
console.log(a); // 55
console.log(b); // 44
codingtute.com
JavaScript ES6 Cheat Sheet
@codingtute
JS
console.log(a) // prints 3 const entries = Object.entries(obj);
/* returns an array of [key, value]
Promises with finally
Const pairs of the object passed */
promise
// can be assigned only once console.log(entries);
.then((result) => { ··· })
const a = 55 /* prints
.catch((error) => { ··· })
a = 44 // throws an error [
.finally(() => { /* logic
['firstName', 'FirstName'],
independent of success/error */ })
Multiline string ['lastName', 'LastName'],
/* The handler is called when the
console.log(` ['age', 24],
promise is fulflled or rejected.*/
This is a ['country', 'India']
multiline string ]; */
`)
String includes()
/* {firstName: "FirstName", lastName: "LastName2",
console.log('apple'.includes('pl')) canSing: true} */
// prints true /* great for modifying objects without side
console.log('apple'.includes('tt')) effects/affecting the original */
// prints false
String startsWith()
console.log('apple'.startsWith('ap')) Destructuring Nested Objects
//prints true const Person = {
console.log('apple'.startsWith('bb')) name: "Harry Potter",
//prints false age: 29,
sex: "male",
String repeat() materialStatus: "single",
console.log('ab'.repeat(3)) address: {
//prints "ababab"
country: "USA",
state: "Nevada",
Destructuring array
city: "Carson City",
let [a, b] = [3, 7];
console.log(a); // 3 pinCode: "500014",
console.log(b); // 7 },
};
Destructuring object const { address : { state, pinCode }, name } = Person;
let obj = { console.log(name, state, pinCode)
a: 55, // Harry Potter Nevada 500014
b: 44 console.log(city) // ReferenceError
};
let { a, b } = obj;
console.log(a); // 55
console.log(b); // 44
codingtute.com
JavaScript loops @codingtute
for for...in
for (let i = 0; i < 5; i++) const arr = [3, 5, 7];
JS
{ arr.foo = 'hello';
console.log(i); for (let i in arr) {
} console.log(i);
//Prints 0 1 2 3 4 }
//Prints "0", "1", "2", "foo"
do-while
let iterator = 0;
do { for...of
iterator++; const arr = [3, 5, 7];
console.log(iterator); for (let i of arr) {
} while (iterator < 5); console.log(i);
//Prints 1 2 3 4 5 }
//Prints 3, 5, 7
while
let iterator = 0;
while (iterator < 5) {
iterator++;
console.log(iterator);
}
//Prints 1 2 3 4 5
codingtute.com
JavaScript loops @codingtute
for for...in
for (let i = 0; i < 5; i++) const arr = [3, 5, 7];
JS
{ arr.foo = 'hello';
console.log(i); for (let i in arr) {
} console.log(i);
//Prints 0 1 2 3 4 }
//Prints "0", "1", "2", "foo"
do-while
let iterator = 0;
do { for...of
iterator++; const arr = [3, 5, 7];
console.log(iterator); for (let i of arr) {
} while (iterator < 5); console.log(i);
//Prints 1 2 3 4 5 }
//Prints 3, 5, 7
while
let iterator = 0;
while (iterator < 5) {
iterator++;
console.log(iterator);
}
//Prints 1 2 3 4 5
codingtute.com
JavaScript Arrays Destructuring
@codingtute
JS
const [a, b, c] = [123, 'second', true];
// a => 123
// b => 'second'
// c => true
Skipping items
const [, b] = [123, 'second', true];
// b => 'second'
Swapping variables
let x = true;
let y = false;
[x, y] = [y, x];
// x => false
// y => true
codingtute.com
JavaScript Arrays Destructuring
@codingtute
JS
const [a, b, c] = [123, 'second', true];
// a => 123
// b => 'second'
// c => true
Skipping items
const [, b] = [123, 'second', true];
// b => 'second'
Swapping variables
let x = true;
let y = false;
[x, y] = [y, x];
// x => false
// y => true
codingtute.com
JavaScript Async/Await
@codingtute
JS
Async
When we append the keyword “async” to the function, this function
returns the Promise by default on execution. Async keyword provides
extra information to the user of the function:
The function contains some Asynchronous Execution
The returned value will be the Resolved Value for the Promise.
Await
The keyword "await" makes JavaScript wait until that promise settles
and returns its result
The 'await' works only inside async functions
codingtute.com
JavaScript Async/Await
@codingtute
JS
Async
When we append the keyword “async” to the function, this function
returns the Promise by default on execution. Async keyword provides
extra information to the user of the function:
The function contains some Asynchronous Execution
The returned value will be the Resolved Value for the Promise.
Await
The keyword "await" makes JavaScript wait until that promise settles
and returns its result
The 'await' works only inside async functions
codingtute.com
JavaScript API Calls Cheat Sheet
JS
@codingtute
JS
@codingtute
<script $(document).ready(function(){
src="https://cdn.jsdelivr.net/npm/axios/dis $.ajax({
t/axios.min.js"></script> url:
"https://jsonplaceholder.typicode.com/todos",
axios.get("https://jsonplaceholder.typicode type: "GET",
success: function(result){
.com/todos") console.log(result);
.then(response =>{
console.log(response.data) }
}) })
})
codingtute.com
JWT Token Generation @codingtute
JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and
self-contained way for securely transmitting information between parties as a
JSON object. JSON Web Tokens consist of three parts separated by dots (.),
which are: Header, Payload and, Signature. Therefore, a JWT typically looks
like the following.
xxxxx.yyyyy.zzzzz
base64UrlEncode base64UrlEncode
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva
G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
codingtute.com
JWT Token Generation @codingtute
JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and
self-contained way for securely transmitting information between parties as a
JSON object. JSON Web Tokens consist of three parts separated by dots (.),
which are: Header, Payload and, Signature. Therefore, a JWT typically looks
like the following.
xxxxx.yyyyy.zzzzz
base64UrlEncode base64UrlEncode
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva
G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
codingtute.com
ES6 HTML DOM codingtute.com
@codingtute
codingtute.com
for security purpose. content and opens a stream to which the
Eg. document.domain new document contents may be written.
embeds[]: Synonym for plugins[]. It is the Returns nothing.
array of objects that represent data Eg. document.open()
embedded in the document write(): Inserts the specified string in
Eg. document.embeds[0],document.embeds[1] the document.
fgColor: This property defines the default Eg. document.write()
text color for the document. writeln(): Same as write() but inserts a
Eg. document.fgColor new line after it is done appending.
forms[]: It is the array of forms object one Eg. document.writeln()
for each, as it appears in the form.
Eg. document.forms[0],document.forms[1]
Document properties in W3C DOM
images[]: It is the array of form objects,
one for each element with <img> tag as it
body: Contents of the tag.
appears in the form.
Eg. document.body
Eg. document.images[0],document.images[1]
defaultView: Represents the window in
lastModified: This property defines date of
which the document is displayed.
the most recent update.
Eg. document.defaultView
Eg. document.lastModified
documentElement: Reference to the tag of
linkColor: This property defines the color
the document.
of unvisited links it is the opposite of the
Eg. document.documentElement
vlinkColor.
implementation: Represents the
Eg. document.linkColor
DOMImplementation object that represents
links[]: Document link array.
the implementation that created this
Eg. document.links[0],document.links[1]
document.
Location: This property holds the URL of the
Eg. document.implementation
document.
Eg. document.location
codingtute.com
document of the element with the mentioned Id. parentWindow: Document containing window.
Eg. document.getElementById(Id) Eg. document.parentWindow
getElementsByName(name): Returns an array of readyState: Specifies the loading status
nodes with the specified name from the of the document.
document. Eg. document.readyState
Eg. document.getElementsByName(name) uninitialized: Document has not yet
getElementsByTagName(tagname): Returns an started loading.
array of all element nodes in the document Eg. document.uninitialized
that have a specified tagname. loading: Document is loading
Eg. document.getElementsByTagName(tagname) Eg. document.loading
importNode(importedNode, deep): Creates and interactive: Document has loaded
returns a copy of a node from some other sufficiently for the user to interact.
document that is suitable for insertion into Eg. document.interactive
this document. If the deep argument is true, complete: Document has loaded.
it recursively copies the children of the node Eg. document.complete
too.
Eg. document.importNode(importedNode, deep) Document methods in IE4 DOM