Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
123 views

JavaScript Cheat Sheet

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 also demonstrates functions, arrays, objects, DOM manipulation, events, asynchronous JavaScript, AJAX requests, classes, modules, and more - with one or two lines of code to illustrate each concept. The document aims to serve as a quick reference guide for JavaScript keywords and best practices.

Uploaded by

ammalap sree
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

JavaScript Cheat Sheet

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 also demonstrates functions, arrays, objects, DOM manipulation, events, asynchronous JavaScript, AJAX requests, classes, modules, and more - with one or two lines of code to illustrate each concept. The document aims to serve as a quick reference guide for JavaScript keywords and best practices.

Uploaded by

ammalap sree
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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 (+, -, *, /, %, ++, --, =, ==, ===, !=, - JSON.stringify
!==, &&, ||, !, etc.) - JSON.parse
- 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 console.log
- 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) {
console.log(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");
console.log(greeting);
// Array
let fruits = ["apple", "banana", "orange"];
6. Arrays
let fruits = ["apple", "banana", "orange"];
// Object
let person = {
// Accessing elements
name: "Alice",
console.log(fruits[0]); // Output: "apple"
age: 25,
isStudent: false
// Adding elements
};
fruits.push("grape");
2. Basic Math Operations
// Removing elements
let x = 10;
fruits.pop();
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;
console.log(person.name); // Output: "John"
if (grade >= 90) {
// Adding properties
console.log("A");
person.city = "New York";
} else if (grade >= 80) {
console.log("B");
// Modifying properties
} else {
person.age = 31;
console.log("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++) {
document.getElementById("demo");
console.log(i);
}
paragraph.textContent = "Updated return `Hello, my name is ${this.name}
paragraph"; and I am ${this.age} years old.`;
}
9. Event Handling }
// HTML: <button id="btn">Click
me</button> let person = new Person("Alice", 25);
let button = console.log(person.greet()); // Output:
document.getElementById("btn"); "Hello, my name is Alice and I am 25 years
button.addEventListener("click", function() { old."
alert("Button clicked!");
}); 14. Local Storage
// Store data
10. Asynchronous JavaScript (Promises) localStorage.setItem("username", "John");
function fetchData() {
return new Promise((resolve, reject) => { // Retrieve data
// Simulate fetching data from a server let username =
setTimeout(() => { localStorage.getItem("username");
let data = [1, 2, 3, 4, 5]; console.log(username); // Output: "John"
resolve(data);
}, 2000); // Remove data
}); localStorage.removeItem("username");
}
15. Regular Expressions
fetchData().then((data) => { let str = "Hello, my email is
console.log(data); john@example.com";
}).catch((error) => { let pattern = /[\w.-]+@[a-z]+\.[a-z]+/;
console.error(error); let result = str.match(pattern);
}); console.log(result); // Output:
["john@example.com"]
11. AJAX with Fetch API
16. Error Handling (Try-Catch)
fetch('https://jsonplaceholder.typicode.com/p try {
osts/1') // Code that may throw an error
.then(response => response.json()) let result = 10 / 0;
.then(data => console.log(data)) } catch (error) {
.catch(error => console.error(error)); console.error("Error:", error.message);
}
12. ES6 Arrow Functions
let add = (a, b) => a + b; 17. ES6 Modules (Export and Import)
console.log(add(2, 3)); // Output: 5 // math.js
export function add(a, b) {
13. ES6 Classes return a + b;
class Person { }
constructor(name, age) {
this.name = name; // main.js
this.age = age; import { add } from './math.js';
} console.log(add(2, 3)); // Output: 5

greet() {
Happy Learning !

You might also like