Created
August 31, 2024 17:16
-
-
Save zachary/4993224d6dc7e15ff1dfbf0c835269c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variable Declarations | |
let x;// Declares a block-scoped variable | |
const y = 10; // Declares a block-scoped. read-only constant | |
// Functions | |
function myFunction() { | |
//Function declaration | |
} | |
const myFunction = () = { | |
//Arrow unction exeression | |
} | |
// Conditional Statements | |
if (condition) { | |
// Executes if condition is true | |
}else if (otherCondition) { | |
// Executes if otherCondition is true | |
} else { | |
// Executes if all conditions are false | |
} | |
// Loops | |
for (let i = 0; i < 10; i++) { | |
// For loop; repeats code while a condition is true | |
} | |
while (condition) { | |
// While loop; repeats code while condition is true | |
} | |
do { | |
//Executes code block at least once, then repeats while condition is true | |
} while (condition); | |
// Array Methods | |
let arr = [1, 2, 3]; | |
arr.push(4); // Adds an element to the end of the array | |
arr.pop(); // Removes the last element from the array | |
arr.shift(); // Removes the first element from the array | |
arr.unshift(0); // Adds an element to the beginning of the array | |
// Object Methods | |
let obj = { name: 'John', age: 30 }; | |
Object.keys(obj);// Returns an array of the object's keys | |
Object.values(obj ); // Returns an array of the object's values | |
Object.entries(obj); // Returns an array of the object's key-value pairs | |
// String Methods | |
let str = "Hello, world!"; | |
str.length; // Returns the length of the string | |
str.toUpperCase(); // Converts string to uppercase | |
str.toLowerCase(); // Converts string to lowercase | |
str.includes("world"); // Checks if the string contains "world" | |
//DOM Manipulation | |
document.getElementById('id'); // Selects an element by its ID | |
document.querySelector('.class'); // Selects the first element that matches the CSS selector | |
document.querySelectorAll( 'div'); // Selects all elements that match the CSS selector | |
document.createElement('div'); //creates a new HTML element | |
// Event Listeners | |
element.addEventListener ('click', function); // Attaches a click event listener to an element | |
//JSON Methods | |
JSON.stringify(obj); // Converts a JavaScript object to a JSON string | |
JSON.parse(jsonStr); // Parses a JSON string into a JavaScript object | |
// Asynchronous Operations | |
fetch(url) | |
.then response = response.json()) // Performs a network request and returns a promise | |
•then(data = console.log(data)) | |
•catch(error = console.error(error)); // Catches errors in the fetch process | |
async function fetchData(){ | |
// Declares an asynchronous function | |
try { | |
let response = await fetch(url): // Waits for the fetch to complete | |
let data = await response.json(); // Waits for the response to be parsed | |
console. log(data); // Logs the data | |
} catch (error) { | |
console.error(error); // Catches and logs any errors | |
} | |
} | |
// Date and Time | |
let now = new Date(); // Creates a new date object representing the current date and | |
now.getFullYear(); // Gets the four-digit year | |
now.getMonth(); // Gets the month (0-11, January is 0) | |
now.getDate(); // Gets the day of the month (1-31) | |
now.getHours(); // Gets the hour (0-23) | |
now.getMinutes(); // Gets the minutes(0-59) | |
now.getSeconds(); // Gets the seconds (0-59) | |
// Error Handling | |
try { | |
//Attempts to execute code that may throw an error | |
} catch (error) { | |
// Handles the error | |
console.error(error); // Logs the error to the console | |
} finally { | |
// Executes code after try and catch, regardless of the result | |
} | |
// Module Imports and Exports | |
export const myFunction = () => {/* ... */}; // Exports a function from a module | |
import { myFunction} from './myModule'; // Imports a function from a module | |
// JavaScript Class | |
class Person { | |
constructor(name, age) { // Constructor method for initializing an object | |
this. name = name; | |
this.age = age; | |
} | |
greet() { | |
// Method to greet | |
console. log(`Hello, my name is ${this.name}`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment