JavaScript Interview Questions and Answers | Set-2 - GeeksforGeeks

05 Nov, 2019
We have already discussed some questions in JavaScript Interview Questions and Answers | Set-1 . Below are some more related questions:What are all the looping structures in JavaScript ?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.    How can style/class of an element be changed ?To change the style/class of an element there are two possible ways.document.getElementById("myText").style.fontSize = "16px;document.getElementById("myText").className = "class";    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)
    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”.Geeks = 42;
Geeks = "GeeksforGeeks";    How to convert the string of any base to integer in JavaScript ?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.    Explain how to detect the operating system on the client machine ?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.    What are the types of Pop up boxes available in JavaScript ?There are three types of pop boxes available in JavaScript.AlertConfirmPrompt    What is the difference between an alert box and a confirmation box ?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.    What is the disadvantage of using innerHTML in JavaScript ?There are lots of disadvantages of using the innerHTML in JavaScript like 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.    What is the use of void(0) ?The void(0) is used to call another method without refreshing the page during the calling time parameter “zero” will be passed.    What are JavaScript Cookies ?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 computer. When cookies were invented, they were basically little documents containing information about you and your preferences. For instance, when you select your 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.    How to create a cookie using JavaScript ?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";    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.    How to delete a cookie using JavaScript ?Deleting 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.    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 ignores and behave like a normal character.// Need escape character
document.write("GeeksforGeeks: A Computer Science Portal "for Geeks" ")
document.write("GeeksforGeeks: A Computer Science Portal \"for Geeks\" ")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.    Whether JavaScript has a concept level scope ?JavaScript is not concept level scope, its declared the variable inside any function has scope inside the function.    How generic objects can be created in JavaScript ?To create a generic object in JavaScript usevar I = new object();    Which keywords are used to handle exceptions ?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.    What is the use of the blur function ?It is used to remove focus from the selected element. This method starts the blur event or it can be attached a function to run when a blur event occurs.    What is the unshift method in JavaScript ?It is used to insert elements in the front of an array. It is like a push method that insert elements at the beginning of the array.
We have already discussed some questions in JavaScript Interview Questions and Answers | Set-1 . Below are some more related questions:
What are all the looping structures in JavaScript ?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.    
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.
    
How can style/class of an element be changed ?To change the style/class of an element there are two possible ways.document.getElementById("myText").style.fontSize = "16px;document.getElementById("myText").className = "class";    
document.getElementById("myText").style.fontSize = "16px;
document.getElementById("myText").style.fontSize = "16px;
document.getElementById("myText").className = "class";
document.getElementById("myText").className = "class";
    
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)
    
The readFile() functions is used for reading operation.readFile( Path, Options, Callback)
readFile( Path, Options, Callback)
The writeFile() functions is used for writing operation.writeFile( Path, Data, Callback)

writeFile( Path, Data, Callback)

    
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”.Geeks = 42;
Geeks = "GeeksforGeeks";    
Geeks = 42;
Geeks = "GeeksforGeeks";
    
How to convert the string of any base to integer in JavaScript ?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.    
    
Explain how to detect the operating system on the client machine ?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.    
    
What are the types of Pop up boxes available in JavaScript ?There are three types of pop boxes available in JavaScript.AlertConfirmPrompt    
Alert
Confirm
Prompt
    
What is the difference between an alert box and a confirmation box ?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.    
    
What is the disadvantage of using innerHTML in JavaScript ?There are lots of disadvantages of using the innerHTML in JavaScript like 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.    
    
What is the use of void(0) ?The void(0) is used to call another method without refreshing the page during the calling time parameter “zero” will be passed.    
    
What are JavaScript Cookies ?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 computer. When cookies were invented, they were basically little documents containing information about you and your preferences. For instance, when you select your 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.    
    
How to create a cookie using JavaScript ?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";    
document.cookie = "key1 = value1; key2 = value2; expires = date";
    
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.    
    
How to delete a cookie using JavaScript ?Deleting 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.    
    
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 ignores and behave like a normal character.// Need escape character
document.write("GeeksforGeeks: A Computer Science Portal "for Geeks" ")
document.write("GeeksforGeeks: A Computer Science Portal \"for Geeks\" ")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.    
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 ignores and behave like a normal character.// Need escape character
document.write("GeeksforGeeks: A Computer Science Portal "for Geeks" ")
document.write("GeeksforGeeks: A Computer Science Portal \"for Geeks\" ")
// Need escape character
document.write("GeeksforGeeks: A Computer Science Portal "for Geeks" ")
document.write("GeeksforGeeks: A Computer Science Portal \"for Geeks\" ")
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.
    
Whether JavaScript has a concept level scope ?JavaScript is not concept level scope, its declared the variable inside any function has scope inside the function.    
    
How generic objects can be created in JavaScript ?To create a generic object in JavaScript usevar I = new object();    
var I = new object();
    
Which keywords are used to handle exceptions ?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.    
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.
    
What is the use of the blur function ?It is used to remove focus from the selected element. This method starts the blur event or it can be attached a function to run when a blur event occurs.    
    
What is the unshift method in JavaScript ?It is used to insert elements in the front of an array. It is like a push method that insert elements at the beginning of the array.
interview-preparation
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills