JavaScript Basics Cheat Sheet
JavaScript Basics Cheat Sheet
The spread operator ('...') is used to copy elements from one array or properties from an object to another array or object, creating a shallow copy. This prevents alterations to the original data structure as it duplicates values into a new structure, but nested objects or arrays may still reference the original unless deep copied .
JavaScript arrays are flexible due to dynamic size and built-in methods for modifying elements. Methods such as 'push' add elements to the end of the array, and 'pop' removes the last element. These methods allow easy alteration of array contents without the need for complex manipulations .
Objects in JavaScript offer advantages in structured data storage through key-value pairs and ease of access through dot or bracket notation (e.g., person.name). They are suited for encapsulating complex data entities and logic through methods. Utilizing methods, they support data encapsulation and reusability, vital for efficiently managing and modeling application state or user information .
Template literals simplify multi-line strings and embedded expressions using backticks (` `) and ${expression}, making the code more readable and reducing errors related to string concatenation. For example, traditional concatenation: 'Hello, ' + name; vs. template literals: `Hello, ${name}`. Template literals allow easier integration of variables and expressions directly in the string .
'for...of' loops are advantageous for iterating over iterable objects like arrays because they simplify syntax by directly accessing array values instead of indices, making the code cleaner and less error-prone, especially for reading or iterating items. This loop automatically handles the counter variable, reducing manual errors .
Block scope with 'let' and 'const' keeps variables defined within a specific block, i.e., between curly braces, influencing variable lifespan to only exist within such blocks. This contrasts function scope with 'var', where variables persist throughout entire functions. Block scope reduces unintended variable sharing outside of intended code blocks, preventing side effects in larger applications .
Function declarations are hoisted, meaning they are available before their definition in the code, and use the 'function' keyword to define their scope. Arrow functions, introduced in ES6, use a shorter syntax and do not have their own 'this' context, which binds 'this' lexically rather than dynamically, making them ideal for non-method functions but unsuitable for 'this'-dependent methods .
Event handlers allow JavaScript to respond to user actions or browser events, making webpages interactive. For instance, a button can trigger a function using 'onclick', updating DOM elements or displaying alerts. Example: <button onclick="sayHello()">Click</button> triggers sayHello() that executes alert('Hello!') upon the click event, enhancing user interaction .
'let' allows block-scoped variable declaration that can be reassigned, making it useful for variables that will change value within a block. 'const', also block-scoped, is used for variables that should remain constant after being initialized, enhancing code stability by preventing reassignment. 'var' is function-scoped or globally-scoped if declared outside a function, which can lead to unexpected behavior due to its lack of block scope .
Destructuring allows for concise syntax to extract values from objects and arrays. For objects, it allows assigning multiple properties to variables at once, e.g., let { name, age } = person. For arrays, it allows extracting multiple elements, e.g., let [a, b] = array. This enhances code efficiency by reducing the need for multiple lines of assignment and aligns variable names directly from the structure .