The Ultimate JavaScript Cheat Sheet
I`ll cover key concepts, syntax, and examples for fundamental topics. Please note that this will be an overview, and you may need to refer to the official documentation for more in-depth information.
JavaScript Cheat Sheet
1. **Variables and Data Types:**
- Declare variables using `var`, `let`, or `const`.
- JavaScript has dynamic typing.
- Data types include String, Number, Boolean, Object, Array, and Null/Undefined.
let name = "John";
const age = 25;
var isStudent = true;
2. **Operators:**
- Arithmetic, Comparison, Logical, and Assignment operators.
- Ternary operator for concise conditionals.
let result = (5 + 3) * 2;
let isEqual = 10 === "10";
let isLoggedIn = true;
let message = isLoggedIn ? "Welcome" : "Please log in";
3. **Control Flow:**
- Use `if`, `else if`, and `else` for conditional statements.
- `switch` for multi-case scenarios.
- `for`, `while`, and `do-while` for loops.
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
switch (value) {
case 1:
// code for case 1
break;
case 2:
// code for case 2
break;
default:
// code if no cases match
}
for (let i = 0; i < 5; i++) {
// code to repeat
}
4. **Functions:**
- Define functions using the `function` keyword.
- Functions can take parameters and return values.
function add(a, b) {
return a + b;
}
let result = add(3, 4);
5. **Arrays:**
- Ordered collections of values.
- Use array methods like `push`, `pop`, `shift`, `unshift`, `slice`, `splice`, etc.
let fruits = ['apple', 'orange', 'banana'];
fruits.push('grape');
let removed = fruits.pop();
6. **Objects:**
- Unordered collections of key-value pairs.
- Access values using dot notation or bracket notation.
let person = {
name: 'John',
age: 30,
isStudent: false
};
console.log(person.name); // Output: John
7. **Classes:**
- Object-oriented programming in JavaScript.
- Create classes and instantiate objects.
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
start() {
console.log("Engine started");
}
}
let myCar = new Car('Toyota', 'Camry');
myCar.start();
8. **DOM Manipulation:**
- Interact with the Document Object Model (DOM) to change HTML content.
- Use methods like `getElementById`, `querySelector`, `innerHTML`, etc.
document.getElementById('myElement').innerHTML = 'New Content';
9. **Events:**
- Respond to user interactions.
- Use event listeners to handle events.
document.getElementById('myButton').addEventListener('click', function() {
// code to execute on button click
});
10. **Asynchronous JavaScript:**
- Handle asynchronous operations using callbacks, promises, and async/await.
- Use `fetch` for API calls.
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
***Conclusion***
This cheat sheet provides a brief overview of essential JavaScript concepts. For more detailed information, refer to the official documentation on the Mozilla Developer Network (MDN) or other reliable sources.
Comments
Post a Comment