0% found this document useful (0 votes)
2 views

JavaScript_Cheatsheet

This JavaScript cheatsheet provides a comprehensive overview of key concepts including variables, data types, operators, conditional statements, loops, functions, arrays, objects, DOM manipulation, error handling, ES6+ features, asynchronous JavaScript, JSON, and miscellaneous methods. It includes examples for each topic to illustrate usage. This resource serves as a quick reference for developers to understand and implement JavaScript functionalities.

Uploaded by

skygennetsis14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaScript_Cheatsheet

This JavaScript cheatsheet provides a comprehensive overview of key concepts including variables, data types, operators, conditional statements, loops, functions, arrays, objects, DOM manipulation, error handling, ES6+ features, asynchronous JavaScript, JSON, and miscellaneous methods. It includes examples for each topic to illustrate usage. This resource serves as a quick reference for developers to understand and implement JavaScript functionalities.

Uploaded by

skygennetsis14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

JavaScript Cheatsheet

1. Variables and Data Types

- Declaring Variables:
let x = 5; // Block-scoped variable
const y = 10; // Constant value (cannot be reassigned)
var z = 20; // Function-scoped variable (legacy)

- Data Types:
- String: 'Hello', "World"
- Number: 42, 3.14
- Boolean: true, false
- Object: { key: "value", age: 25 }
- Array: [1, 2, 3, 4]
- Null: null
- Undefined: undefined
- Symbol: Symbol('unique')

2. Operators

- Arithmetic Operators:
+, -, *, /, %, ++, --

- Comparison Operators:
==, === (strict), !=, !== (strict), >, <, >=, <=

- Logical Operators:
&& (AND), || (OR), ! (NOT)

- Assignment Operators:
=, +=, -=, *=, /=, %=, **=

- Ternary Operator:
condition ? expr1 : expr2;
3. Conditional Statements

- If-Else:
if (condition) {
// Code to execute
} else {
// Code to execute
}

- Switch Statement:
switch (expression) {
case value1:
// Code to execute
break;
case value2:
// Code to execute
break;
default:
// Code to execute
}

4. Loops

- For Loop:
for (let i = 0; i < 5; i++) {
console.log(i); // Output: 0, 1, 2, 3, 4
}

- While Loop:
let i = 0;
while (i < 5) {
console.log(i); // Output: 0, 1, 2, 3, 4
i++;
}

- Do-While Loop:
let i = 0;
do {
console.log(i); // Output: 0, 1, 2, 3, 4
i++;
} while (i < 5);

5. Functions

- Function Declaration:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice"));

- Function Expression:
const add = function(x, y) {
return x + y;
};
console.log(add(2, 3));

- Arrow Functions:
const multiply = (x, y) => x * y;
console.log(multiply(3, 4));

6. Arrays

- Creating an Array:
let fruits = ["Apple", "Banana", "Cherry"];

- Accessing Array Elements:


console.log(fruits[0]); // "Apple"

- Array Methods:
fruits.push("Grapes"); // Adds element to the end
fruits.pop(); // Removes last element
fruits.shift(); // Removes first element
fruits.unshift("Orange"); // Adds element to the beginning

7. Objects

- Creating an Object:
let person = {
name: "John",
age: 30,
greet: function() {
return "Hello!";
}
};

- Accessing Object Properties:


console.log(person.name); // "John"
console.log(person["age"]); // 30

8. DOM Manipulation

- Select an Element:
const element = document.getElementById("myElement");
const elements = document.querySelectorAll(".myClass");

- Modify Content:
element.innerHTML = "New content!";

- Add Event Listeners:


element.addEventListener("click", function() {
alert("Element clicked!");
});

9. Error Handling

- Try-Catch Block:
try {
let result = riskyOperation();
} catch (error) {
console.error("An error occurred:", error);
}

10. ES6+ Features

- Let and Const (Block-scoped):


let x = 10;
const y = 20;

- Template Literals:
let name = "Alice";
let greeting = `Hello, ${name}!`;

- Destructuring Assignment:
const person = { name: "John", age: 30 };
const { name, age } = person;

- Spread Operator:
let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5];

- Rest Parameters:
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}

- Promises:
let promise = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve("Success!");
} else {
reject("Failure");
}
});

promise.then(result => console.log(result)).catch(error => console.error(error));

11. Asynchronous JavaScript

- Async/Await:
async function fetchData() {
try {
let response = await fetch("https://api.example.com/data");
let data = await response.json();
console.log(data);
} catch (error) {
console.error("Error fetching data:", error);
}
}
fetchData();

12. JSON (JavaScript Object Notation)

- Convert Object to JSON:


let jsonString = JSON.stringify({ name: "John", age: 30 });

- Convert JSON to Object:


let obj = JSON.parse('{"name": "John", "age": 30}');

13. Miscellaneous

- Math Methods:
Math.random(); // Random number between 0 and 1
Math.floor(3.14); // 3
Math.ceil(3.14); // 4

- Date Methods:
let date = new Date();
console.log(date.getFullYear());

You might also like