Category: JavaScript interview questions
-
What is the Difference Between preventDefault and stopPropagation in JavaScript?
In JavaScript, preventDefault() and stopPropagation() are two methods used to control the behavior of events, but they serve distinct purposes. Here’s a detailed explanation of their differences: preventDefault() Purpose: Prevents the browser’s default action associated with an event. Use Case: Use preventDefault() when you want to stop the default behavior of an event, such as…
-
What is the difference between window and document?
In JavaScript, window and document are both objects that provide access to different parts of the browser’s functionality. Here’s a detailed explanation of their differences: window object document object Summary of Differences Feature window document Represents Browser window/tab Web page content (DOM) Scope Top-level object Part of the window object Usage General browser-related actions DOM…
-
How do you decode or encode a URL in JavaScript?
In JavaScript, you can use the encodeURIComponent, encodeURI, decodeURIComponent, and decodeURI functions to encode or decode URLs or their components. 1. Encoding a URL Use encodeURI or encodeURIComponent to encode URLs or parts of them. 2. Decoding a URL Use decodeURI or decodeURIComponent to decode previously encoded URLs or components. 3. Key Differences Between encodeURI…
-
How do you redeclare variables in a switch block without an error
If you try to redeclare variables in a switch block then it will cause errors because there is only one block. For example, the below code block throws a syntax error as below, To avoid this error, you can create a nested block inside a case clause and create a new block scoped lexical environment.
-
What is a unary function?
A unary function is a function that accepts exactly one argument. The term “unary” comes from the prefix “uni-“, meaning “one.” Unary functions are commonly used in mathematics and programming. Characteristics of a Unary Function Example of Unary Function
-
What is a higher order function?
A higher-order function in JavaScript is a function that either: Higher-order functions are a core concept in functional programming and are widely used in JavaScript for tasks like iteration, transformations, and handling asynchronous operations. Characteristics of Higher-Order Functions Examples of Higher-Order Functions Taking Functions as Arguments The classic example is the array methods like map,…
-
What is a first order function?
A first-order function is a function that does not take other functions as arguments or return functions as its result. It operates directly on values and performs computations or operations on them. Characteristics of First-Order Functions Example of First-Order Functions add is a first-order function because it takes two numbers as input and returns a…
-
What is a first-class function?
In JavaScript, a first-class function refers to the ability of functions to be treated like any other value. This means functions in JavaScript can: These properties make functions “first-class citizens” in JavaScript. Key Characteristics of First-Class Functions Assigning to Variables Passing as Arguments Returning Functions Storing in Data Structures Why First-Class Functions are Powerful Higher-Order…
-
What is the difference between call, apply and bind in JavaScript?
In JavaScript, this refers to the object that is currently executing the function. Its value depends on how the function is called. However, there are situations where you might want to explicitly set the value of this. This is where call, apply, and bind come into play. 1. The call Method The call method invokes…
-
What are the possible ways to create an Object in JavaScript?
There are many ways to create the JavaScript object. 1. Object literal syntax In JavaScript, an object literal is a straightforward way to define objects using key-value pairs. Here’s an example: Object literal property values can be of any data type, including array, function, and nested object. 2. Object constructor The Object constructor is a…