JavaScript programming language cheat sheet.
- User
JavaScript programming language cheat sheet.
Assistant
JavaScript Cheat Sheet - A Comprehensive Guide
This cheat sheet covers essential JavaScript concepts, syntax, and common functions. It's organized for quick reference.
I. Basic Syntax & Data Types
-
Comments:
// Single-line comment/* Multi-line comment */
-
Variables:
var(older, function-scoped)let(block-scoped, can be reassigned)const(block-scoped, cannot be reassigned)let x = 10;const PI = 3.14159;
-
Data Types:
- Primitive:
Number:10,3.14,-5String:"Hello",'World'Boolean:true,falseNull:null(represents intentional absence of a value)Undefined:undefined(variable declared but not assigned a value)Symbol:Symbol('description')(unique and immutable identifier)BigInt:123456789012345678901234567890n(for integers larger than Number can represent)
- Object:
Object:{ name: "John", age: 30 }Array:[1, 2, 3]
- Primitive:
-
Operators:
- Arithmetic:
+,-,*,/,%(modulo),**(exponentiation) - Assignment:
=,+=,-=,*=,/=,%= - Comparison:
==(loose equality),===(strict equality),!=(loose inequality),!==(strict inequality),>,<,>=,<= - Logical:
&&(AND),||(OR),!(NOT) - Ternary:
condition ? valueIfTrue : valueIfFalse
- Arithmetic:
II. Control Flow
-
if...elseStatements:if (condition) { // Code to execute if condition is true } else if (anotherCondition) { // Code to execute if anotherCondition is true } else { // Code to execute if all conditions are false } -
switchStatement:switch (expression) { case value1: // Code to execute if expression equals value1 break; case value2: // Code to execute if expression equals value2 break; default: // Code to execute if expression doesn't match any case } -
forLoop:for (initialization; condition; increment/decrement) { // Code to execute repeatedly } -
whileLoop:while (condition) { // Code to execute repeatedly as long as condition is true } -
do...whileLoop:do { // Code to execute at least once } while (condition); -
for...inLoop: (Iterates over object properties)for (let key in object) { // Code to execute for each property key } -
for...ofLoop: (Iterates over iterable objects like arrays, strings, Maps, Sets)for (let value of iterable) { // Code to execute for each value }
III. Functions
-
Function Declaration:
function functionName(parameters) { // Code to execute return value; // Optional return statement } -
Function Expression:
const functionName = function(parameters) { // Code to execute return value; }; -
Arrow Function (ES6+):
const functionName = (parameters) => { // Code to execute return value; }; // Single expression - implicit return const functionName = (parameters) => value; -
Parameters & Arguments:
parametersare defined in the function declaration.arguments(in older functions) is an array-like object containing all arguments passed to the function. (Less common with arrow functions)
-
returnStatement: Exits the function and optionally returns a value. -
Function Scope:
varhas function scope,letandconsthave block scope. -
Higher-Order Functions: Functions that can take other functions as arguments or return functions. Examples:
map,filter,reduce.
IV. Objects
-
Object Literals:
const person = { name: "Alice", age: 25, city: "New York", greet: function() { console.log("Hello, my name is " + this.name); } }; -
Accessing Properties:
object.property(dot notation)object["property"](bracket notation - useful for dynamic property names)
-
Adding/Modifying Properties:
object.newProperty = value;object.property = newValue;
-
Deleting Properties:
delete object.property;
-
thisKeyword: Refers to the object that the function is being called on. Its value depends on how the function is called. -
Object Methods: Functions that are properties of an object.
-
Constructor Functions: Used to create objects using the
newkeyword. -
Classes (ES6+): A syntactical sugar over prototype-based inheritance.
class Person { constructor(name, age) { this.name = name; this.age = age; } greet() { console.log("Hello, my name is " + this.name); } } const person = new Person("Bob", 40); person.greet();
V. Arrays
-
Array Literals:
const myArray = [1, 2, 3, "hello", true]; -
Accessing Elements:
array[index](zero-based indexing)
-
Array Methods:
push(element): Adds an element to the end of the array.pop(): Removes the last element from the array.shift(): Removes the first element from the array.unshift(element): Adds an element to the beginning of the array.splice(startIndex, deleteCount, item1, item2, ...): Modifies the array by removing or replacing elements.slice(startIndex, endIndex): Returns a new array containing a portion of the original array.concat(array1, array2, ...): Returns a new array by concatenating the original array with the other arrays.join(separator): Converts the array to a string.indexOf(element): Returns the index of the first occurrence of an element.includes(element): Returnstrueif the array contains the element.forEach(callbackFunction): Executes a provided function once for each array element.map(callbackFunction): Creates a new array with the results of calling a provided function on every element in the calling array.filter(callbackFunction): Creates a new array with all elements that pass the test implemented by the provided function.reduce(callbackFunction, initialValue): Reduces the array to a single value.sort(compareFunction): Sorts the array elements.reverse(): Reverses the order of the array elements.
VI. DOM Manipulation
-
documentObject: Represents the HTML document. -
Selecting Elements:
document.getElementById(id): Gets an element by its ID.document.getElementsByClassName(className): Gets elements by their class name (returns an HTMLCollection).document.getElementsByTagName(tagName): Gets elements by
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.