JavaScript Cheat Sheet with Examples

0% found this document useful (0 votes)
157 views4 pages
This JavaScript cheat sheet document provides concise examples for common JavaScript concepts and best practices. It covers basics like variables, data types, operators, and conditionals. It…

Uploaded by

ammalap sree
  • Cover
  • Table of Contents
  • Loops
  • Functions
  • Variables and Data Types
  • Conclusion
  • Asynchronous JavaScript
  • Events and Error Handling

JavaScript cheat sheet document for quick 7.

Error Handling
remember keywords and best practices few - try...catch
examples: JavaScript Cheat Sheet - throw

1. Basics 8. Local Storage


- Variables (var, let, const) - Saving and Retrieving Data
- Data Types (Number, String, Boolean,
Object, Array, null, undefined) 9. JSON (JavaScript Object Notation)
- Operators (+, -, *, /, %, ++, --, =, ==, ===, !=, - [Link]
!==, &&, ||, !, etc.) - [Link]
- Conditionals (if, else if, else, switch)
- Loops (for, while, do-while, for...in, for...of) 10. Regular Expressions
- Creating Patterns
2. Functions - Matching and Replacing Text
- Declaring Functions
- Function Expressions 11. Scope and Closures
- Arrow Functions - Global Scope
- Parameters and Arguments - Function Scope
- Return Statement - Block Scope
- Higher-order Functions (map, filter, reduce, - Closures
etc.)
12. Classes and Object-Oriented Programming
3. Arrays - Class Declaration
- Array Declaration - Constructor
- Accessing Elements - Methods
- Adding and Removing Elements - Inheritance (extends, super)
- Array Methods (push, pop, shift, unshift,
splice, slice, etc.) 13. Fetch API
- Making HTTP Requests
4. Objects
- Object Literal 14. Modules (ES6)
- Accessing Object Properties - Exporting and Importing Modules
- Adding and Modifying Properties
- Nested Objects 15. Browsers and Window Object
- Object Methods - Browser Information (navigator)
- Popup Dialogs (alert, confirm, prompt)
5. DOM Manipulation
- Selecting Elements (getElementById, 16. Debugging JavaScript
querySelector, etc.) - Using [Link]
- Changing HTML Content (innerHTML, - Browser Developer Tools
textContent, etc.)
- Modifying Styles (style property) 17. Best Practices
- Adding and Removing Classes (classList) - Avoiding Global Variables
- Handling Events (addEventListener) - Writing Readable Code
- Handling Asynchronous Code
6. Asynchronous JavaScript - Performance Tips
- setTimeout and setInterval
- Promises
- async/await
JavaScript cheat sheet document with few
examples for each concept: // While Loop
let count = 1;
JavaScript Cheat Sheet with Examples while (count <= 5) {
[Link](count);
1. Variables and Data Types
count++;
// Number
}
let age = 30;
5. Functions
// String
function greet(name) {
let name = "John";
return "Hello, " + name + "!";
}
// Boolean
let isStudent = true;
let greeting = greet("Alice");
[Link](greeting);
// Array
let fruits = ["apple", "banana", "orange"];
6. Arrays
let fruits = ["apple", "banana", "orange"];
// Object
let person = {
// Accessing elements
name: "Alice",
[Link](fruits[0]); // Output: "apple"
age: 25,
isStudent: false
// Adding elements
};
[Link]("grape");
2. Basic Math Operations
// Removing elements
let x = 10;
[Link]();
let y = 5;
7. Objects
let sum = x + y;
let person = {
let difference = x - y;
name: "John",
let product = x * y;
age: 30,
let quotient = x / y;
isStudent: true
let remainder = x % y;
};
3. Conditional Statements
// Accessing properties
let grade = 85;
[Link]([Link]); // Output: "John"
if (grade >= 90) {
// Adding properties
[Link]("A");
[Link] = "New York";
} else if (grade >= 80) {
[Link]("B");
// Modifying properties
} else {
[Link] = 31;
[Link]("C");
}
8. DOM Manipulation
// HTML: <p id="demo">This is a
4. Loops
paragraph.</p>
// For Loop
let paragraph =
for (let i = 1; i <= 5; i++) {
[Link]("demo");
[Link](i);
}
[Link] = "Updated return `Hello, my name is ${[Link]}
paragraph"; and I am ${[Link]} years old.`;
}
9. Event Handling }
// HTML: <button id="btn">Click
me</button> let person = new Person("Alice", 25);
let button = [Link]([Link]()); // Output:
[Link]("btn"); "Hello, my name is Alice and I am 25 years
[Link]("click", function() { old."
alert("Button clicked!");
}); 14. Local Storage
// Store data
10. Asynchronous JavaScript (Promises) [Link]("username", "John");
function fetchData() {
return new Promise((resolve, reject) => { // Retrieve data
// Simulate fetching data from a server let username =
setTimeout(() => { [Link]("username");
let data = [1, 2, 3, 4, 5]; [Link](username); // Output: "John"
resolve(data);
}, 2000); // Remove data
}); [Link]("username");
}
15. Regular Expressions
fetchData().then((data) => { let str = "Hello, my email is
[Link](data); john@[Link]";
}).catch((error) => { let pattern = /[\w.-]+@[a-z]+\.[a-z]+/;
[Link](error); let result = [Link](pattern);
}); [Link](result); // Output:
["john@[Link]"]
11. AJAX with Fetch API
16. Error Handling (Try-Catch)
fetch('[Link] try {
osts/1') // Code that may throw an error
.then(response => [Link]()) let result = 10 / 0;
.then(data => [Link](data)) } catch (error) {
.catch(error => [Link](error)); [Link]("Error:", [Link]);
}
12. ES6 Arrow Functions
let add = (a, b) => a + b; 17. ES6 Modules (Export and Import)
[Link](add(2, 3)); // Output: 5 // [Link]
export function add(a, b) {
13. ES6 Classes return a + b;
class Person { }
constructor(name, age) {
[Link] = name; // [Link]
[Link] = age; import { add } from './[Link]';
} [Link](add(2, 3)); // Output: 5

greet() {
Happy Learning !

JavaScript cheat sheet document for quick 
remember keywords and best practices few 
examples: JavaScript Cheat Sheet
JavaScript cheat sheet document with few 
examples for each concept: 
JavaScript Cheat Sheet with Examples 
1. Variables
paragraph.textContent = "Updated 
paragraph"; 
 
9. Event Handling 
   // HTML: <button id="btn">Click 
me</button>

You might also like