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

JavaScript Cheat Shee Chat Gptt

This document is a comprehensive JavaScript cheat sheet covering basic syntax, operators, control flow, functions, objects, arrays, DOM manipulation, promises, async/await, and ES6 features. It provides code snippets and explanations for each topic, making it a useful reference for JavaScript developers. Key concepts include variable declarations, conditional statements, loops, and modern ES6 syntax like classes and template literals.

Uploaded by

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

JavaScript Cheat Shee Chat Gptt

This document is a comprehensive JavaScript cheat sheet covering basic syntax, operators, control flow, functions, objects, arrays, DOM manipulation, promises, async/await, and ES6 features. It provides code snippets and explanations for each topic, making it a useful reference for JavaScript developers. Key concepts include variable declarations, conditional statements, loops, and modern ES6 syntax like classes and template literals.

Uploaded by

ayushnono4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

# JavaScript Cheat Sheet

## **Basic Syntax**
```javascript
// Single-line comment
/* Multi-line comment */

// Variables
let x = 10; // Block-scoped
const pi = 3.14; // Constant
var y = 20; // Function-scoped (old
syntax)

// Data types
let num = 5; // Number
let str = "Hello"; // String
let isTrue = true; // Boolean
let arr = [1, 2, 3]; // Array
let obj = { key: "value" }; // Object
let und; // Undefined
let n = null; // Null
```

---

## **Operators**
- Arithmetic: `+`, `-`, `*`, `/`, `%`, `**`
- Assignment: `=`, `+=`, `-=`, `*=`
- Comparison: `==`, `!=`, `===`, `!==`, `>`, `<`,
`>=`, `<=`
- Logical: `&&`, `||`, `!`
- Ternary: `condition ? expr1 : expr2`

---

## **Control Flow**
```javascript
// Conditional Statements
if (x > 0) {
console.log("Positive");
} else if (x < 0) {
console.log("Negative");
} else {
console.log("Zero");
}

// Switch Case
switch (color) {
case "red":
console.log("Stop");
break;
case "green":
console.log("Go");
break;
default:
console.log("Caution");
}

// Loops
for (let i = 0; i < 5; i++) {
console.log(i);
}
let i = 0;
while (i < 5) {
console.log(i);
i++;
}

do {
console.log(i);
i++;
} while (i < 5);
```

---

## **Functions**
```javascript
// Function Declaration
function add(a, b) {
return a + b;
}
// Function Expression
const subtract = function (a, b) {
return a - b;
};

// Arrow Function
const multiply = (a, b) => a * b;

// Default Parameters
function greet(name = "Guest") {
return `Hello, ${name}!`;
}
```

---

## **Objects**
```javascript
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
greet: function () {
return `Hi, I am ${this.firstName}`;
},
};

console.log(person.greet());
```

---

## **Arrays**
```javascript
let numbers = [1, 2, 3, 4, 5];

// Array Methods
numbers.push(6); // Add to end
numbers.pop(); // Remove from end
numbers.shift(); // Remove from start
numbers.unshift(0); // Add to start
numbers.forEach(num =>
console.log(num)); // Iterate
```

---

## **DOM Manipulation**
```javascript
// Selecting Elements
let element =
document.getElementById("id");
let elements =
document.querySelectorAll(".class");

// Modifying Content
element.textContent = "New Text";
element.innerHTML = "<strong>Bold
Text</strong>";

// Event Listeners
element.addEventListener("click", () => {
alert("Element clicked!");
});
```

---

## **Promises and Async/Await**


```javascript
// Promises
let promise = new Promise((resolve,
reject) => {
let success = true;
if (success) {
resolve("Success!");
} else {
reject("Error!");
}
});
promise.then(response =>
console.log(response)).catch(error =>
console.log(error));

// 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);
}
}
```

---

## **ES6 Features**
- Template Literals: `` `Hello, ${name}` ``
- Destructuring: `let { x, y } = obj;`
- Spread Operator: `let arr2 = [...arr1];`
- Classes:
```javascript
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a
noise.`);
}
}

class Dog extends Animal {


speak() {
console.log(`${this.name} barks.`);
}
}
```

You might also like