JavaScript Interview Questions (Part3)

Why extending array is a bad idea?

Extending built-in objects like arrays by adding methods to their prototypes can lead to unexpected behavior and compatibility issues with other code or libraries. It's better to create utility functions that operate on arrays instead of modifying their prototypes directly.

How do you check if an object is an array or not?

You can use the Array.isArray() method to check if an object is an array. For example: Array.isArray(myObject) returns true if myObject is an array.

Can you assign an anonymous function to a variable?

Yes, you can assign an anonymous function (also known as a function expression) to a variable. For example: const myFunction = function() { /* function body */ };

Difference between private, public, and static variables?

In object-oriented programming, private variables are only accessible within the class, public variables can be accessed from outside the class, and static variables are shared among all instances of the class, not tied to a specific instance.

What is differential inheritance?

Differential inheritance is a concept in JavaScript where an object can directly inherit properties and methods from another object, serving as its prototype. Any changes to the prototype object are immediately reflected in the inheriting object.

What is the use of the blur function in JavaScript?

The blur() function in JavaScript is used to remove focus from the current focused element. It's often used in form validation scenarios to trigger validation checks when a user navigates away from an input field.

What is NaN?

NaN stands for "Not-a-Number" and is a special value in JavaScript representing an undefined or unrepresentable value resulting from a mathematical operation that doesn't yield a valid number.

What is the difference between ViewState and SessionState?

ViewState is used to store small amounts of data on the client-side between postbacks, while SessionState is used to store data on the server-side for a specific user session across multiple requests.

How to enable 'Strict' mode in JavaScript?

You can enable strict mode in JavaScript by adding the following line at the beginning of a script or a function: "use strict";. Strict mode helps catch common coding mistakes and makes JavaScript behave more predictably.

Is there automatic type conversion in JavaScript?

Yes, JavaScript supports automatic type conversion, also known as type coercion. It automatically converts data from one type to another when performing operations, which can sometimes lead to unexpected results.

What will happen if an infinite while loop is run in JavaScript?

If an infinite while loop is run in JavaScript, the browser or environment will become unresponsive, and the script will essentially freeze, causing high CPU usage and preventing other tasks from being executed.

What is the unshift method in JavaScript?

The unshift() method in JavaScript is used to add one or more elements to the beginning of an array. It modifies the original array and returns the new length of the array after adding the elements.

What are the decodeURI() and encodeURI() functions?

decodeURI() is used to decode a URI that has been encoded with encodeURI(). These functions help manage and manipulate URI components while preserving special characters.

Difference between window.onload and onDocumentReady?

window.onload waits for all assets (including images) to load before firing, while onDocumentReady (using libraries like jQuery) triggers when the DOM is ready, which may be before all assets are loaded.

What are JavaScript Cookies?

JavaScript cookies are small pieces of data stored on the user's browser. They can be used to store information such as user preferences or session data.

Can you access Cookies using JavaScript?

Yes, JavaScript can access cookies using the document.cookie property, which contains a string of all cookies associated with the current document.