Javascript Flashcards
What is JavaScript?
JavaScript is a high-level, dynamic, untyped, and interpreted programming language that is primarily used for creating interactive effects within web browsers. It enables developers to implement complex features on web pages.
Is JavaScript a compiled or interpreted language?
JavaScript is primarily an interpreted language, meaning that its code is executed line by line at runtime by the JavaScript engine in the browser. However, modern JavaScript engines use Just-In-Time (JIT) compilation techniques to improve performance.
What is the purpose of the ‘var’ keyword in JavaScript?
‘var’ is a keyword used to declare a variable in JavaScript. Variables declared with ‘var’ have function scope or global scope, depending on where they are defined. It’s important to note that ‘var’ is hoisted, meaning that it can be referenced before its declaration.
What are the differences between ‘let’ and ‘const’ in JavaScript?
‘let’ is used to declare block-scoped variables, meaning they are only accessible within the block they are defined. ‘const’ is also block-scoped but is used to declare variables that cannot be reassigned after their initial assignment. Both ‘let’ and ‘const’ are not hoisted in the same way as ‘var’.
What is a JavaScript function?
A function in JavaScript is a reusable block of code that performs a specific task. Functions can take parameters, which are values passed into them, and can return a value. They can be defined using function declarations or function expressions.
What is the difference between a function declaration and a function expression?
A function declaration defines a function with a name and can be called before its definition due to hoisting. A function expression, on the other hand, defines a function as part of an expression and cannot be called before it is defined.
What are JavaScript objects?
JavaScript objects are collections of key-value pairs where keys are strings (or Symbols) and values can be any data type, including functions. Objects are used to store and organize data in a structured way, allowing for complex data manipulation.
What is JSON and how is it related to JavaScript?
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is a text format that is completely language-independent but uses conventions that are familiar to programmers of the C family of languages, including JavaScript.
Explain the concept of ‘this’ in JavaScript.
‘this’ refers to the context in which a function is executed. In a global context, ‘this’ refers to the global object (window in browsers). Inside a method, ‘this’ refers to the object the method is called on. In constructor functions, ‘this’ refers to the new object being created.
What is a closure in JavaScript?
A closure is a function that retains access to its lexical scope, even when the function is executed outside that scope. This means that a closure can remember the environment in which it was created, allowing for data encapsulation and privacy.
What is event delegation in JavaScript?
Event delegation is a technique that allows a single event listener to manage events for multiple elements.
It’s a way you can add an event listner once for multiple elements with support for adding extra children.
Instead of attaching event listeners to each individual element, an event listener is attached to a parent element, which captures events bubbling up from its children.
What are promises in JavaScript?
Promises are objects that represent the eventual completion or failure of an asynchronous operation and its resulting value. A promise can be in one of three states: pending, fulfilled, or rejected. They provide a cleaner alternative to callback functions for handling asynchronous code.
What is the purpose of the ‘async’ and ‘await’ keywords?
The ‘async’ keyword is used to declare an asynchronous function, which allows the use of ‘await’ inside it. The ‘await’ keyword pauses the execution of the async function until the promise is resolved, making asynchronous code appear more synchronous and easier to read.
What is the difference between ‘==’ and ‘===’ in JavaScript?
’==’ is the equality operator that checks for value equality after performing type coercion, while ‘===’ is the strict equality operator that checks for both value and type equality without coercion. Using ‘===’ is generally recommended to avoid unexpected results.
What are JavaScript arrays?
JavaScript arrays are list-like objects that can hold multiple values in a single variable. They are dynamic in nature, meaning they can grow or shrink in size and can store elements of any type, including other arrays or objects.
What is the purpose of the ‘map’ method in JavaScript arrays?
The ‘map’ method creates a new array populated with the results of calling a provided function on every element in the calling array. It does not modify the original array and is useful for transforming data.
What is the ‘filter’ method in JavaScript?
The ‘filter’ method creates a new array with all elements that pass the test implemented by the provided function. It is used to remove elements from an array based on specific criteria without modifying the original array.
What is the purpose of the ‘reduce’ method in JavaScript?
The ‘reduce’ method executes a reducer function on each element of the array, resulting in a single output value. It is particularly useful for performing operations that combine array elements, such as summing values or flattening arrays.
What is the difference between synchronous and asynchronous programming?
Synchronous programming executes tasks sequentially, blocking subsequent tasks until the current task is completed. Asynchronous programming allows tasks to run concurrently, enabling other operations to continue while waiting for a task to finish, which improves efficiency and responsiveness.
What is the role of the ‘DOMContentLoaded’ event?
‘DOMContentLoaded’ is an event that fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. It is often used to execute JavaScript code that manipulates the DOM.
What is a JavaScript module?
A JavaScript module is a piece of code that is executed once it is loaded, and it can export variables and functions to be used in other modules. Modules help in organizing code, avoiding global scope pollution, and promoting reusability.
What are template literals in JavaScript?
Template literals are string literals that allow embedded expressions, multi-line strings, and string interpolation using backticks (`). They provide a more flexible way to create strings compared to traditional single or double quotes.
What is the purpose of the ‘fetch’ API in JavaScript?
The ‘fetch’ API is a modern interface for making HTTP requests in JavaScript. It returns a promise that resolves to the response of the request. ‘fetch’ is used to retrieve data from a server and can handle various types of requests, including GET and POST.
What are arrow functions in JavaScript?
Arrow functions are a shorter syntax for writing function expressions in JavaScript. They do not have their own ‘this’ context, which means they inherit ‘this’ from the surrounding lexical scope. This makes them particularly useful in methods and callbacks.
What is the purpose of the ‘bind’ method in JavaScript?
The ‘bind’ method creates a new function that, when called, has its ‘this’ keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. It is useful for ensuring that functions have the correct ‘this’ context.
What is the difference between ‘call’ and ‘apply’ methods in JavaScript?
‘call’ and ‘apply’ are methods used to invoke functions with a specified ‘this’ context. The difference lies in how arguments are passed: ‘call’ takes arguments individually, while ‘apply’ takes an array of arguments.
What is the purpose of the ‘setTimeout’ function?
‘setTimeout’ is a built-in JavaScript function that executes a specified function after a defined number of milliseconds. It is commonly used for delaying actions or executing code asynchronously after a certain period.
What is the purpose of the ‘setInterval’ function?
‘setInterval’ is a built-in JavaScript function that repeatedly calls a specified function with a fixed time delay between each call. It continues to execute until it is stopped using ‘clearInterval’.
What is the ‘window’ object in JavaScript?
The ‘window’ object represents the browser’s window and serves as the global object in the client-side JavaScript environment. It provides access to the browser’s features and allows manipulation of the document displayed in the browser.
What are JavaScript events?
JavaScript events are actions or occurrences that happen in the browser, such as clicks, key presses, or mouse movements. Events can be detected and responded to through event listeners, allowing developers to create interactive web applications.
What is the purpose of the ‘addEventListener’ method?
‘addEventListener’ is a method that attaches an event handler to a specified element. It allows developers to listen for specific events and execute a function when that event occurs, enabling dynamic interactions on web pages.
What is the ‘localStorage’ API in JavaScript?
The ‘localStorage’ API allows web applications to store data in the browser persistently. Data stored in ‘localStorage’ is accessible across sessions and remains until explicitly deleted. It provides a simple key-value storage mechanism for client-side data.