Cheatography
https://cheatography.com
Closure
function externalFunction(x) {
return function internalFunction(y) {
return x + y;
};
}
const add5 = externalFunction(5);
console.log(add5(3)); // Output: 8
|
Allows internal functions to access external function variables even after the external function has executed.
Class
class Animal {
constructor(name) {
this.name = name;
}
talk() {
console.log("generic sound");
}
}
class Dog extends Animal {
constructor(name) {
super(name);
}
talk() {
console.log("Bau!");
}
}
|
Fetch
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
|
|
|
Prototype
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log(Hello, I'm ${this.name} );
};
const person1 = new Person("Alice");
person1.greet(); // Output: Hello, I'm Alice
|
JSON Stringify
const myObj= {
name: "Alice",
age: 30,
city: "Rome",
interests: ["programming", "travel"]
};
const JSONstring = JSON.stringify(myObj);
console.log(JSONstring );
// Output: {"name":"Alice","age":30,"city":"Rome","interests":["programming","travel"]}
console.log(typeof JSONstring); // Output: string
|
JSON.stringify() to convert a JavaScript object to a JSON string.
JSON Parse
const jsonString = '{"name":"Bob","age":25,"city":"New York","interests":["music","sport"]}';
const jsonObj = JSON.parse(jsonString);
console.log(jsonObj );
// Output: { name: 'Bob', age: 25, city: 'New York', interests: [ 'music', 'sport' ] }
console.log(jsonObj.name); // Output: Bob
console.log(jsonObj.interests[0]); // Output: music
console.log(typeof jsonObj); // Output: object
|
JSON.parse() to convert a JSON string into a JavaScript object.
|
Created By
https://www.techelopment.it
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by techelopment