Javascript Flashcards

1
Q

What is JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is JavaScript a compiled or interpreted language?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of the ‘var’ keyword in JavaScript?

A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the differences between ‘let’ and ‘const’ in JavaScript?

A

‘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’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a JavaScript function?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between a function declaration and a function expression?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are JavaScript objects?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is JSON and how is it related to JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain the concept of ‘this’ in JavaScript.

A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a closure in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is event delegation in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are promises in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the ‘async’ and ‘await’ keywords?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between ‘==’ and ‘===’ in JavaScript?

A

’==’ 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are JavaScript arrays?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of the ‘map’ method in JavaScript arrays?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the ‘filter’ method in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the purpose of the ‘reduce’ method in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the difference between synchronous and asynchronous programming?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the role of the ‘DOMContentLoaded’ event?

A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is a JavaScript module?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What are template literals in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the purpose of the ‘fetch’ API in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are arrow functions in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the purpose of the ‘bind’ method in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the difference between ‘call’ and ‘apply’ methods in JavaScript?

A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the purpose of the ‘setTimeout’ function?

A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is the purpose of the ‘setInterval’ function?

A

‘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’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is the ‘window’ object in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What are JavaScript events?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the purpose of the ‘addEventListener’ method?

A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is the ‘localStorage’ API in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is the ‘sessionStorage’ API in JavaScript?

A

The ‘sessionStorage’ API is similar to ‘localStorage’ but is limited to the duration of the page session. Data stored in ‘sessionStorage’ is accessible only within the same tab or window and is cleared when the tab or window is closed.

34
Q

What are regular expressions in JavaScript?

A

Regular expressions (regex) are patterns used to match character combinations in strings. They are used for searching, replacing, and validating strings based on specific criteria. JavaScript provides the ‘RegExp’ object to work with regex.

35
Q

What is the purpose of the ‘try…catch’ statement?

A

‘try…catch’ is a statement used to handle exceptions in JavaScript. Code that may throw an error is placed inside the ‘try’ block, and if an error occurs, control is passed to the ‘catch’ block, allowing developers to handle errors gracefully.

36
Q

What is the difference between ‘undefined’ and ‘null’ in JavaScript?

A

‘undefined’ is a primitive value that indicates a variable has been declared but has not yet been assigned a value. ‘null’ is also a primitive value that represents the intentional absence of any object value. Both indicate the absence of a value but in different contexts.

37
Q

What are higher-order functions in JavaScript?

A

Higher-order functions are functions that can take other functions as arguments or return functions as their results. They are a key feature of functional programming and enable powerful abstractions in JavaScript.

38
Q

What is the purpose of the ‘spread’ operator?

A

The ‘spread’ operator (three dots: …) allows an iterable such as an array to be expanded in places where zero or more arguments or elements are expected. It is useful for copying, concatenating, and passing arrays as function arguments.

39
Q

What is the ‘rest’ operator in JavaScript?

A

The ‘rest’ operator (also three dots: …) is used to represent an indefinite number of arguments as an array. It allows functions to accept any number of parameters and is often used in function definitions to handle variable-length argument lists.

40
Q

What are the different data types in JavaScript?

A

JavaScript has several data types, including primitive types such as undefined, null, boolean, number, string, and symbol. Additionally, there are complex types like objects and arrays. Understanding these types is fundamental to working effectively with JavaScript.

41
Q

What is the purpose of the ‘console’ object?

A

The ‘console’ object provides access to the browser’s debugging console. It offers methods such as ‘console.log()’ for logging information, ‘console.error()’ for error messages, and ‘console.warn()’ for warnings, making it easier to debug JavaScript code.

42
Q

What are ES6 features in JavaScript?

A

ES6 (ECMAScript 2015) introduced several new features to JavaScript, including arrow functions, classes, template literals, destructuring, promises, and the ‘let’ and ‘const’ keywords. These features enhance the language’s capabilities and improve code readability.

43
Q

What is the purpose of the ‘import’ and ‘export’ statements in JavaScript?

A

‘import’ and ‘export’ statements are used in ES6 modules to share code between different modules. ‘export’ allows a module to expose functions, objects, or values, while ‘import’ allows another module to access those exports.

44
Q

What is the significance of the ‘prototype’ in JavaScript?

A

In JavaScript, every object has a prototype that acts as a template from which the object inherits properties and methods. The prototype chain allows for inheritance, enabling objects to share functionality and reducing redundancy.

45
Q

What is the purpose of the ‘Object.create()’ method?

A

‘Object.create()’ is a method that creates a new object with the specified prototype object and properties. It allows for the creation of objects with a specific prototype, facilitating inheritance in JavaScript.

46
Q

What is functional programming in JavaScript?

A

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state or mutable data. In JavaScript, it emphasizes the use of higher-order functions, pure functions, and immutability.

47
Q

What is the purpose of the ‘Object.assign()’ method?

A

‘Object.assign()’ is a method that copies the values of all enumerable properties from one or more source objects to a target object. It is often used to merge objects or create shallow copies of objects.

48
Q

What are the different ways to create an object in JavaScript?

A

There are several ways to create objects in JavaScript, including using object literals, constructor functions, the ‘new’ keyword with built-in objects, and the ‘Object.create()’ method. Each method has its own use cases and advantages.

49
Q

What is the purpose of the ‘slice()’ method in JavaScript arrays?

A

The ‘slice()’ method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included). It does not modify the original array and is useful for extracting subarrays.

50
Q

What is the ‘join()’ method in JavaScript arrays?

A

The ‘join()’ method creates and returns a new string by concatenating all elements of an array, separated by a specified separator string. If no separator is provided, the default separator is a comma.

51
Q

What is the purpose of the ‘splice()’ method in JavaScript arrays?

A

The ‘splice()’ method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. It can be used to modify the original array directly.

52
Q

What is the significance of ‘strict mode’ in JavaScript?

A

Strict mode is a way to opt-in to a restricted variant of JavaScript, which helps catch common coding errors and unsafe actions, such as defining global variables accidentally. It is declared by adding ‘use strict’; at the top of a script or function.

53
Q

What is the purpose of the ‘Math’ object in JavaScript?

A

The ‘Math’ object provides properties and methods for mathematical constants and functions. It includes functions for trigonometry, logarithms, and random number generation, making it easier to perform complex mathematical calculations.

54
Q

What are the different ways to iterate over an array in JavaScript?

A

There are several methods to iterate over an array in JavaScript, including using loops (for, while), the ‘forEach’ method, the ‘map’ method, and the ‘for…of’ loop. Each method has its own syntax and use cases.

55
Q

What is the purpose of the ‘filter’ method in JavaScript arrays?

A

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.

56
Q

What is the ‘window.onload’ event?

A

‘window.onload’ is an event that fires when the entire page, including all dependent resources such as stylesheets and images, has finished loading. It is often used to execute code that requires the complete DOM and resources to be available.

57
Q

What is the ‘Promise.all()’ method?

A

‘Promise.all()’ is a method that takes an iterable of promises and returns a single promise that resolves when all of the promises in the iterable have resolved, or rejects with the reason of the first promise that rejects. It is useful for executing multiple asynchronous operations in parallel.

58
Q

What is the ‘Promise.race()’ method?

A

‘Promise.race()’ is a method that takes an iterable of promises and returns a single promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with its value or reason. It allows for handling the first completed promise among multiple promises.

59
Q

What is the ‘window.localStorage’ API?

A

The ‘window.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.

60
Q

What is the ‘window.sessionStorage’ API?

A

The ‘window.sessionStorage’ API is similar to ‘localStorage’ but is limited to the duration of the page session. Data stored in ‘sessionStorage’ is accessible only within the same tab or window and is cleared when the tab or window is closed.

61
Q

What is the ‘navigator’ object in JavaScript?

A

The ‘navigator’ object contains information about the browser being used and the operating system. It provides properties and methods to detect browser features, user language, and other relevant data for web applications.

62
Q

What is the ‘document’ object in the context of the DOM?

A

The ‘document’ object is the entry point into the Document Object Model (DOM) and represents the entire HTML document loaded in the browser. It provides methods and properties for accessing and manipulating the content and structure of the web page.

63
Q

What is the ‘innerHTML’ property used for?

A

The ‘innerHTML’ property is used to get or set the HTML content inside an element. It allows developers to dynamically update the content of an element, including adding or removing HTML elements.

64
Q

What is the ‘style’ property in JavaScript?

A

The ‘style’ property allows access to the inline CSS styles of an element. It can be used to modify the appearance of elements dynamically by changing their styles through JavaScript.

65
Q

What is the ‘addEventListener’ method used for?

A

‘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.

66
Q

What is the ‘removeEventListener’ method?

A

‘removeEventListener’ is a method used to remove an event handler that was previously added to an element using ‘addEventListener’. It requires the same event type and function reference to successfully remove the listener.

67
Q

What is the ‘for…of’ loop in JavaScript?

A

The ‘for…of’ loop is a modern JavaScript loop that allows iteration over iterable objects such as arrays, strings, and maps. It provides a simpler syntax compared to traditional loops and automatically iterates over the values of the iterable.

68
Q

What is the ‘for…in’ loop in JavaScript?

A

The ‘for…in’ loop is used to iterate over the enumerable properties of an object. It iterates over the keys of the object, making it useful for accessing object properties, but it should not be used for arrays due to potential unexpected behavior.

69
Q

What is the ‘Object.keys()’ method?

A

‘Object.keys()’ is a method that returns an array of a given object’s own enumerable property names. It is useful for iterating over the properties of an object and can be combined with other array methods for further manipulation.

70
Q

What is the ‘Object.values()’ method?

A

‘Object.values()’ is a method that returns an array of a given object’s own enumerable property values. It allows developers to easily access the values of an object without having to loop through its keys.

71
Q

What is the ‘Object.entries()’ method?

A

‘Object.entries()’ is a method that returns an array of a given object’s own enumerable property [key, value] pairs. It provides a convenient way to work with both keys and values of an object in a structured format.

72
Q

What is the ‘JSON.stringify()’ method?

A

‘JSON.stringify()’ is a method that converts a JavaScript object or value to a JSON string. It is commonly used for serializing data to be sent to a server or stored in local storage.

73
Q

What is the ‘JSON.parse()’ method?

A

‘JSON.parse()’ is a method that parses a JSON string and constructs the JavaScript value or object described by that string. It is used to convert JSON data received from a server back into a usable JavaScript object.

74
Q

What is the ‘setImmediate’ function?

A

‘setImmediate’ is a function that executes a single callback after the current event loop cycle. It is similar to ‘setTimeout’ with a delay of 0 but is designed to execute the callback as soon as possible after I/O events.

75
Q

What is the ‘clearTimeout’ function?

A

‘clearTimeout’ is a built-in JavaScript function that cancels a timeout previously established by ‘setTimeout’. It prevents the callback from executing if it has not yet run.

76
Q

What is the ‘clearInterval’ function?

A

‘clearInterval’ is a built-in JavaScript function that stops the execution of a function that was set to repeat at specified intervals using ‘setInterval’. It halts any further calls to the specified function.

77
Q

What is the ‘Promise’ constructor?

A

The ‘Promise’ constructor is used to create a new Promise object. It takes a function with two arguments, resolve and reject, which are called to either fulfill or reject the promise based on the outcome of an asynchronous operation.

78
Q

What is the ‘Promise.prototype.then()’ method?

A

‘Promise.prototype.then()’ is a method that adds fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called function. It allows chaining of asynchronous operations.

79
Q

What is the ‘Promise.prototype.catch()’ method?

A

‘Promise.prototype.catch()’ is a method that adds a rejection handler to the promise and returns a new promise, making it easier to handle errors in promise chains. It is a cleaner way to handle errors compared to using ‘then()’ with a second argument.

80
Q

What is the significance of the ‘async’ keyword?

A

The ‘async’ keyword is used to declare an asynchronous function, which returns a promise. It allows the use of ‘await’ within the function, making it easier to write and read asynchronous code without deeply nested callbacks.

81
Q

What is the significance of the ‘await’ keyword?

A

The ‘await’ keyword is used inside an async function to pause the execution of the function until the promise is resolved. It allows for writing asynchronous code in a synchronous style, improving readability and maintainability.

82
Q

What is the ‘try…finally’ statement?

A

‘try…finally’ is a statement that allows you to execute a block of code (the ‘try’ block) and ensure that a specified block of code (the ‘finally’ block) runs regardless of whether an error occurs in the try block. It is useful for cleanup operations.