Show Menu
Cheatography

JavaScript Intermediate Cheat Sheet by

JavaScript intermediate

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.s­tri­ngify() 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.p­arse() to convert a JSON string into a JavaScript object.
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          AngularJS Cheat Sheet
          JavaScript Cheat Sheet

          More Cheat Sheets by techelopment

          JavaScript Basics Cheat Sheet