JavaScript Interview Questions and Answers (2024) – Intermediate Level

In this article, you will learn JavaScript interview questions and answers intermediate level that are most frequently asked in interviews. Before proceeding to learn JavaScript interview questions and answers – intermediate level, first we learn the complete JavaScript Tutorial, and JavaScript Interview Questions and Answers – Basic Level.

JavaScript Interview Questions and Answers

  • while loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
  • for loop: A for loop provides a concise way of writing the loop structure. Unlike a while loop, for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping.
  • do while: A do-while loop is similar to while loop with the only difference that it checks the condition after executing the statements, and therefore is an example of Exit Control Loop.

2. How can the style/class of an element be changed?

To change the style/class of an element there are two possible ways. We use document.getElementByID method

document.getElementById("myText").style.fontSize = "16px;
document.getElementById("myText").className = "class";

3. Explain how to read and write a file using JavaScript?

  • The readFile() functions is used for reading operation.
readFile( Path, Options, Callback)
  • The writeFile() functions is used for writing operation.
writeFile( Path, Data, Callback)

4. What is called Variable typing in JavaScript ?

The variable typing is the type of variable used to store a number and using that same variable to assign a “string”.

Beginner = 42;
Beginner = "w3wiki";

In JavaScript, parseInt() function is used to convert the string to an integer. This function returns an integer of base which is specified in second argument of parseInt() function. The parseInt() function returns Nan (not a number) when the string doesn’t contain number.

To detect the operating system on the client machine, one can simply use navigator.appVersion or navigator.userAgent property. The Navigator appVersion property is a read-only property and it returns the string that represents the version information of the browser.

There are three types of pop boxes available in JavaScript.

An alert box will display only one button which is the OK button. It is used to inform the user about the agreement has to agree. But a Confirmation box displays two buttons OK and cancel, where the user can decide to agree or not.

9. What is the disadvantage of using innerHTML in JavaScript?

There are lots of disadvantages of using the innerHTML in JavaScript as the content will replace everywhere. If you use += like “innerHTML = innerHTML + ‘html'” still the old content is replaced by HTML. It preserves event handlers attached to any DOM elements.

The void(0) is used to call another method without refreshing the page during the calling time parameter “zero” will be passed.

Cookies are small files that are stored on a user’s computer. They are used to hold a modest amount of data specific to a particular client and website and can be accessed either by the web server or by the client’s computer. When cookies were invented, they were basically little documents containing information about you and your preferences. For instance, when you select the language in which you want to view your website, the website would save the information in a document called a cookie on your computer, and the next time when you visit the website, it would be able to read a cookie saved earlier.

To create a cookie by using JavaScript you just need to assign a string value to the document.cookie object.

document.cookie = "key1 = value1; key2 = value2; expires = date";

13. How to read a cookie using JavaScript?

The value of the document.cookie is used to create a cookie. Whenever you want to access the cookie you can use the string. The document.cookie string keep a list of name = value pairs separated by semicolons, where name is the name of a cookie and the value is its string value.

14. How to delete a cookie using JavaScript?

Deleting a cookie is much easier than creating or reading a cookie, you just need to set the expires = “past time” and make sure one thing defines the right cookie path unless few will not allow you to delete the cookie.

15. What are escape characters and escape() function?

  • Escape character: This character is required when you want to work with some special characters like single and double quotes, apostrophes, and ampersands. All the special character plays an important role in JavaScript, to ignore that or to print that special character, you can use the escape character backslash “\”. It will normally ignore and behave like a normal character.
// Need escape character
document.write("w3wiki: A Computer Science Portal "for Beginner" ")
document.write("w3wiki: A Computer Science Portal \"for Beginner\" ")
  • escape() function: The escape() function takes a string as a parameter and encodes it so that it can be transmitted to any computer in any network which supports ASCII characters.

16. Whether JavaScript has a concept-level scope?

JavaScript is not concept-level scope, the variables declared inside any function have scope inside the function.

17. How generic objects can be created in JavaScript?

To create a generic object in JavaScript use:

var I = new object();

When executing JavaScript code, errors will almost definitely occur. These errors can occur due to the fault from the programmer’s side due to the wrong input or even if there is a problem with the logic of the program. But all errors can be solved by using the below commands.

  • The try statement lets you test a block of code to check for errors.
  • The catch statement lets you handle the error if any are present.
  • The throw statement lets you make your own errors.

It is used to remove focus from the selected element. This method starts the blur event or it can be attached to a function to run when a blur event occurs.

It is used to insert elements in the front of an array. It is like a push method that inserts elements at the beginning of the array.



Contact Us