Java Script Cheat Sheet
Here's a cheat sheet summarizing some of the common JavaScript concepts, functions, and syntax:
javascript
// JavaScript Cheat Sheet
// Variables
let variableName = value; // Declare a variable with block scope
var variableName = value; // Declare a variable with function scope (deprecated)
const constantName = value; // Declare a constant variable
// Data Types
let number = 10; // Number
let string = "Hello"; // String
let boolean = true; // Boolean
let array = [1, 2, 3]; // Array
let object = { // Object
key1: value1,
key2: value2
};
// Operators
let sum = num1 + num2; // Addition
let difference = num1 - num2; // Subtraction
let product = num1 * num2; // Multiplication
let quotient = num1 / num2; // Division
let remainder = num1 % num2; // Modulus
let isGreater = num1 > num2; // Greater than
let isEqual = num1 === num2; // Equality
let logicalAnd = condition1 && condition2; // Logical AND
let logicalOr = condition1 || condition2; // Logical OR
let logicalNot = !condition; // Logical NOT
// Conditionals
if (condition) {
// Code executed if condition is true
}
else if (condition2) {
// Code executed if condition2 is true
}
else {
// Code executed if no previous conditions are true
}
// Loops
for (let i = 0; i < limit; i++) {
// Code executed in each iteration
}
while (condition) {
// Code executed repeatedly while condition is true
}
do {
// Code executed at least once, then repeatedly while condition is true
} while (condition);
// Functions
function functionName(parameter1, parameter2) {
// Code executed by the function
return value; // Optional return statement
}
// Arrays
let array = [1, 2, 3]; // Array declaration
let length = array.length; // Length of the array
let element = array[index]; // Accessing an element
array.push(value); // Add an element to the end
array.pop(); // Remove the last element
array.unshift(value); // Add an element to the beginning
array.shift(); // Remove the first element
array.splice(index, count); // Remove or replace elements
// Objects
let object = { // Object declaration
key1: value1,
key2: value2,
method: function() {
// Method code
}
};
let value = object.key1; // Accessing a value
object.key3 = value3; // Adding a new key-value pair
delete object.key2; // Deleting a key-value pair
// Events
element.addEventListener('event', function() {
// Code executed when the event occurs
});
// DOM Manipulation
let element = document.getElementById('id'); // Get element by ID
let elements = document.getElementsByClassName('class'); // Get elements by class name
let elements = document.getElementsByTagName('tag'); // Get elements by tag name
element.innerHTML = 'Text'; // Set element's HTML content
element.value = 'Value'; // Set element's value
element.style.property = 'value'; // Set element's CSS style
element.classList.add('class'); // Add a class to an element
element.classList.remove('class'); // Remove a class from an element
// Promises (Asynchronous)
new Promise(function(resolve, reject) {
// Asynchronous code
if (success) {
resolve(result);
} else {
reject(error);
}
}).then(function(result) {
// Code executed on successful promise
}).catch(function(error) {
// Code executed on rejected promise
});
// Fetch API (HTTP Requests)
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(data) {
// Process the response data
})
.catch(function(error) {
// Handle errors
});
// Modules (ES6)
// Exporting module
export const variableName = value;
export function functionName() {
// Code
}
// Importing module
import { variableName, functionName } from './module.js';
This cheat sheet covers some of the commonly used JavaScript concepts, functions, syntax, conditionals, loops, arrays, objects, events, DOM manipulation, promises, Fetch API, and modules. It serves as a handy reference for quick look-ups and reminders while working with JavaScript.
Comments
Post a Comment