JavaScript Cheat Sheet

Here’s a cheat sheet for JavaScript:

Variables

// Declaration
let variableName;

// Initialization
variableName = value;

// Declaration and Initialization
let variableName = value;

// Data Types
let stringVar = "Hello";
let numberVar = 42;
let booleanVar = true;
let arrayVar = [1, 2, 3];
let objectVar = { key: 'value' };

Operators

// Arithmetic Operators
let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a / b;
let remainder = a % b;

// Comparison Operators
let isEqual = a === b;
let isNotEqual = a !== b;
let greaterThan = a > b;
let lessThan = a < b;

// Logical Operators
let andResult = condition1 && condition2;
let orResult = condition1 || condition2;
let notResult = !condition;

Control Flow

// if statement
if (condition) {
  // code to execute if condition is true
} else if (anotherCondition) {
  // code to execute if anotherCondition is true
} else {
  // code to execute if both conditions are false
}

// switch statement
switch (value) {
  case 'case1':
    // code for case1
    break;
  case 'case2':
    // code for case2
    break;
  default:
    // code for default case
}

Loops

// for loop
for (let i = 0; i < array.length; i++) {
  // code to execute in each iteration
}

// while loop
let i = 0;
while (i < 5) {
  // code to execute in each iteration
  i++;
}

// do-while loop
let i = 0;
do {
  // code to execute in each iteration
  i++;
} while (i < 5);

Functions

// Function Declaration
function functionName(parameter1, parameter2) {
  // code to execute
  return result;
}

// Function Expression
const functionName = function(parameter1, parameter2) {
  // code to execute
  return result;
};

// Arrow Function
const functionName = (parameter1, parameter2) => {
  // code to execute
  return result;
};

Arrays

let fruits = ['apple', 'orange', 'banana'];

// Access element
let firstFruit = fruits[0];

// Add element to the end
fruits.push('grape');

// Remove last element
let lastFruit = fruits.pop();

// Iterate through array
for (let fruit of fruits) {
  // code to execute for each element
}

Objects

let person = {
  name: 'John',
  age: 25,
  isStudent: false
};

// Access property
let personName = person.name;

// Modify property
person.age = 26;

// Add new property
person.job = 'Developer';

// Iterate through object
for (let key in person) {
  // code to execute for each property
}

Promises

const fetchData = () => {
  return new Promise((resolve, reject) => {
    // asynchronous code
    if (successCondition) {
      resolve(data);
    } else {
      reject(error);
    }
  });
};

fetchData()
  .then(data => {
    // code to execute on successful promise
  })
  .catch(error => {
    // code to execute on rejected promise
  });

Async/Await

const fetchData = async () => {
  try {
    // asynchronous code
    let data = await asynchronousFunction();
    // code to execute on successful promise
  } catch (error) {
    // code to execute on rejected promise
  }
};

This is a basic JavaScript cheat sheet covering some fundamental concepts. JavaScript is a versatile language, and there’s much more to explore and learn.