JavaScript Interview Questions | Page 2


Question 11:

What is decodeURI() function and explain its usage?

As we know encodeURI() is used to encode the URI, similarly decodeURI is used to decode the encoded URI.

For example:

var uri="http://www.modernpathshala.com/how%20to%20make%20a%20website%20using%20javaScript";
var decodedURI = decodeURI(uri);
console.log(decodedURI);

Output

http://www.modernpathshala.com/how to make a website using javaScript

In above output we can see that variable uri contains uri which is encoded and decodedURI represents the uri which is decoded as shown in above output.

Question 12:

How will you check whether the checkbox is selected or not using JavaScript?

The checked property is used to check whether any checkbox is checked or not. It returns boolean value i.e. true/false

For example

<script type="text/javascript">
    function validate() {
        if (document.getElementById('myCheckbox').checked) {
            alert("Checked");
        } else {
            alert("Not Checked");
        }
    }
</script>
Question 13:

What is difference between undefined variable and undeclared variable?

Variables to which no value is assigned but are declared in the program are known as undefined variables in Javascript whereas variables which are not declared in the program are called undeclared variable in Javascript.

If you try to use undefined variable you will get "undefined" as error message but if you use undeclared variable you will get runtime error as shown in figure:

Undefined vs undeclared variables

Click F12 to launch developers tool on you browser and goto console tab and you can test as shown in image.

Question 14:

How to submit a form using JavaScript by clicking a button?

Generally, you may have submitted form when the user press a submit button. However, sometimes, you need to submit the form programmatically using JavaScript.

JavaScript provides the form object that contains the submit() method. You can get the form using 'id' of the form to get the form object.

For example, if the name of your form is 'myFirstForm', the JavaScript code for the submit call is:

document.forms["myFirstForm"].submit();

The below code snippet illustrates how can a html form can be submitted using JavaScript.

<!DOCTYPE html>
<html>
<head>
<script>
function submitMyForm() {
    document.getElementById("myFirstForm").submit();
}
</script>
</head>
  <body>
   <p>Enter User Name And Password To Login.</p>
     <form id="myFirstForm" action="your_action">
        User name: <input type="text" name="userName"><br>
        Password: <input type="password" name="password"><br><br>
        <input type="button" onclick="submitMyForm()" value="Login">
     </form>
  </body>
</html>
Question 15:

What is push() method in JavaScript?

push() method in JavaScript is used to append/add new elements as the last element of the array. The syntax of push() method is

array.push(element1, ..., elementN);

the method returns the number of elements after adding the elements in the array(length of new array).


Question 16:

What is pop() method in JavaScript?

The pop() method removes the last element from an array and returns that element.

For example:

var arrayOfString = ["JavaScript", "Java", "J2EE"];
arrayOfString.pop();
console.log(arrayOfString);

Output

["JavaScript", "Java"]
Question 17:

Can you make asynchronous call in JavaScript to server? Why is it important?

Yes, we can make asynchronous callin Javascript to server. It is very important aspet of web development as asynchronous call can improve performance of your website at huge extend if used properly. Click to read more about AJAX in detail.

Question 18:

What is the use of atob() method?

The atob() function decodes a string of data which has been encoded using base-64 encoding.

Syntax

var decodedData = window.atob(encodedData);
Question 19:

What is the use of btoa() method?

The btoa() function creates a base-64 encoded ASCII string from a a given string.

Syntax

var encodedData = window.btoa(stringToEncode);
Question 20:

What is role of setTimeout(fn, delay) function in Javascript?

The setTimeout(fn, delay) is used to executes a function, after waiting a specified number of milliseconds.

setTimeout accepts a reference to a function as the first argument and number of milliseconds as second argument.

For example:-

setTimeout(printHello(), 10000);
function printHello(){
    console.log("Hello");
}
Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media