JavaScript Cheatsheet

1. Variables

var x = 10;
let y = 20;
const z = 30;

2. Data Types

let name = 'John';
let age = 25;
let isStudent = true;
let empty = null;
let u;
let uniqueId = Symbol();

const person = { name: 'John', age: 25 };
const arr = [1, 2, 3];
const greet = function() {
  console.log('Hello!');
};

3. Operators

let sum = 10 + 5;

let isEqual = 5 == '5';   // true
let isStrictEqual = 5 === '5'; // false

let result = true && false;  // false

4. Control Flow

if (age > 18) {
  console.log('Adult');
} else {
  console.log('Not an adult');
}

for (let i = 0; i < 5; i++) {
  console.log(i);
}

let i = 0;
while (i < 5) {
  console.log(i);
  i++;
}

5. Functions

function greet(name) {
  return `Hello, ${name}`;
}

const greetArrow = (name) => `Hello, ${name}`;

6. Closures

function outer() {
  let outerVar = 'I am outside!';

  return function inner() {
    console.log(outerVar); // Can access outerVar
  };
}

const innerFunc = outer();
innerFunc(); // 'I am outside!'

7. Objects

const car = {
  make: 'Toyota',
  model: 'Camry',
  year: 2020,
  start: function() {
    console.log('Car started');
  }
};

console.log(car.make);   // 'Toyota'
car.start();  // 'Car started'

8. Arrays

const fruits = ['Apple', 'Banana', 'Orange'];
console.log(fruits[0]);  // 'Apple'

fruits.push('Grapes');  // Adds 'Grapes'
fruits.pop();           // Removes 'Grapes'

fruits.forEach((fruit) => console.log(fruit));

9. ES6 Features

let name = 'John';
let message = `Hello, ${name}!`;

const person = { name: 'John', age: 25 };
const { name, age } = person;

const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];

function greet(name = 'Guest') {
  return `Hello, ${name}`;
}

10. Promises

const fetchData = new Promise((resolve, reject) => {
  setTimeout(() => resolve('Data fetched'), 1000);
});

fetchData.then(data => console.log(data));

11. Async/Await

async function fetchData() {
  const response = await fetch('https://api.example.com/data');
  const data = await response.json();
  console.log(data);
}

fetchData();

12. Error Handling

try {
  let result = riskyOperation();
} catch (error) {
  console.error('Error occurred:', error);
} finally {
  console.log('Cleanup tasks');
}

13. Classes

class Animal {
  constructor(name, type) {
    this.name = name;
    this.type = type;
  }

  makeSound() {
    console.log(`${this.name} makes a sound.`);
  }
}

class Dog extends Animal {
  bark() {
    console.log(`${this.name} barks.`);
  }
}

const dog = new Dog('Rex', 'Dog');
dog.makeSound();  // Rex makes a sound.
dog.bark();       // Rex barks.

14. DOM Manipulation

const element = document.getElementById('myElement');
const elements = document.querySelectorAll('.myClass');

element.textContent = 'New content';

element.addEventListener('click', () => {
  console.log('Element clicked!');
});

element.classList.add('active');

15. Modules

export const name = 'John';
export function greet() {
  console.log('Hello!');
}

import { name, greet } from './module.js';

greet();  // Hello!

16. Destructuring

const person = { name: 'John', age: 30 };
const { name, age } = person;

const [first, second] = [1, 2];

17. Spread and Rest Operators

const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];

function sum(...numbers) {
  return numbers.reduce((a, b) => a + b);
}

18. Set and Map

const mySet = new Set([1, 2, 3]);
mySet.add(4);
console.log(mySet);

const myMap = new Map();
myMap.set('name', 'John');
console.log(myMap.get('name'));

19. Template Literals

const name = 'John';
const message = `Hello, ${name}!`;
console.log(message);

20. JSON Handling

const jsonData = JSON.stringify({ name: 'John', age: 30 });
const obj = JSON.parse(jsonData);
console.log(obj.name);