Key features include dynamic typing, prototype-based object orientation, functions as first-class citizens, and support for event-driven programming.
let and const were introduced in ECMAScript 6. let allows reassignment, while const is used for constant values. var is the older way of declaring variables with function scope.
Single-line comments are written with //, and multi-line comments are enclosed between /* and */.
Closures occur when a function is defined inside another function, allowing the inner function to access the outer function’s variables even after the outer function has completed execution.
The this keyword refers to the current execution context and is determined by how a function is called. It allows access to the object the function is a method of or the object that invoked the function.
Callback functions are functions passed as arguments to other functions and are executed after the completion of an asynchronous operation. They are commonly used in event handling and AJAX requests.
== is an equality operator that performs type coercion, while === is a strict equality operator that checks both value and type.
Asynchronous code is handled using callbacks, Promises, or the modern async/await syntax. Promises provide a cleaner way to handle asynchronous operations.
The setTimeout function is used to delay the execution of a function or code block by a specified time interval, measured in milliseconds.
Promises are objects representing the eventual completion or failure of an asynchronous operation. They simplify asynchronous code and make it more readable.
async/await is a syntax for handling asynchronous code in a more synchronous-like manner. async is used to define an asynchronous function, and await is used to pause the execution until the promise is resolved.
The forEach method is used to iterate over elements in an array. It executes a provided function once for each array element.
The Array.isArray() method is used to check if a variable is an array. It returns true if the variable is an array, otherwise false.
null represents the intentional absence of any object value, while undefined indicates a variable has been declared but not assigned a value.
Elements can be created using document.createElement() and appended to the DOM using methods like appendChild() or insertBefore().
localStorage and sessionStorage are Web Storage APIs used to store key-value pairs in a web browser. localStorage persists data across sessions, while sessionStorage stores data for the duration of a page session.
Arrow functions are a concise syntax for writing function expressions. They do not have their own this and inherit it from the parent scope.
Function declarations are hoisted to the top of their scope and can be called before they are declared. Function expressions, however, are not hoisted.
for…in is used to iterate over the enumerable properties of an object, while for…of is used to iterate over the values of iterable objects like arrays and strings.
The map method creates a new array by applying a provided function to each element of the original array.
The typeof operator is used to determine the type of a variable or expression. It returns a string representing the data type.
The preventDefault() method is used to prevent the default behavior associated with an event, such as form submission or link navigation.
The bind method is used to create a new function with a specified this value and partially applied arguments. It is often used to bind a function to a specific context.
Deep cloning of objects can be achieved using methods like JSON.parse(JSON.stringify(obj)) or libraries like Lodash.
The Object.keys() method returns an array of a given object’s own enumerable property names, allowing you to iterate over the object’s keys.
Template literals are string literals allowing embedded expressions. They are enclosed by backticks ( ) and support multiline strings and variable interpolation.
indexOf returns the index of the first occurrence of a specified value in a string, and lastIndexOf returns the index of the last occurrence.
The parseInt() function or the unary plus operator (+) can be used to convert a string to an integer.
The Same-Origin Policy restricts web pages from making requests to a different domain than the one that served the web page. It is a security measure to prevent cross-site request forgery.
The fetch API is used to make network requests and is more modern than the older XMLHttpRequest. It returns a Promise and provides a simpler way to work with asynchronous data.
Both localStorage and cookies store data on the client side, but cookies are sent with each HTTP request, while localStorage is not. Cookies have size limitations, and data in localStorage is available only to the same origin.
A closure is the combination of a function and the lexical environment within which that function was declared. Closures allow functions to retain access to variables from their outer scope even after the outer function has finished execution.
The splice method is used to add or remove elements from an array. It modifies the original array in place and returns an array of removed elements.
The reduce method is used to accumulate values of an array into a single result. It takes a callback function and an initial value.
Comparing two objects for equality can be done using libraries like Lodash, which provide functions like isEqual, or by manually comparing their properties.
The classList property provides an interface to interact with the class attributes of an HTML element. It allows adding, removing, or toggling classes.
Browser detection can be done using the navigator object. However, feature detection is preferred over browser detection for better compatibility.
The async attribute is used when loading external scripts to make the script execute asynchronously, without blocking the parsing of the HTML document.