JavaScript Interview Questions | Page 3


Question 21:

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

The setInterval(fn, delay) method is used to repeatedly execute given function at given time-interval.

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

For example:-

setInterval(printHello(), 10000);
function printHello(){
    console.log("Hello");
}

If you execute above code snippet Hello will be printed after every 10 seconds.

Question 22:

What is difference between setTimeout and setInterval method in Javascript?

The setTimeout(fn, delay) is used to executes a function, after waiting a specified number of milliseconds whereas the setInterval(fn, delay) method is used to repeatedly execute given function at given time-interval.

Question 23:

What is the difference between disabled and readonly field?

disabled and readonly botha re property of form fields like checkbox, textbox e.t.c. There are some major usage difference between reaadonly and disabled over form field which are listed below:-

Disabled Attribute ReadOnly Attribute
1. If you mark any form field disabled the value of the field is not sent/posted to server. 1. If you mark any form field readOnly the value of the field is sent/posted to server.
2. You cannot focus on form field marked as disabled. 2. You can focus on element marked as readOnly.
3. disabled form field are skipped while tab navigation. 3. readOnly form field are not skipped while tab navigation.
4. disable form field attribute is also available with <SELECT> and <BUTTON> field to prevent user to interact with these controls. 4. readOnly form field attribute is not available with <SELECT> and <BUTTON> field

5. Example:- <input type="text" name="demoField" disabled="disabled">

Disabled Text Box: 

5. Example:- <input type="text" name="demoField" readOnly="true">

Read only Text Box:
Question 24:

How to make any form field disabled using javascript?

You can disable any form field in javascript in below described way:

Syntax:-

formfieldObject.disabled=true|false

Example:-

document.getElementById("myDemoTextField").disabled = true;

Question 25:

 How to make any form field readOnly using javascript?

You can make readOnly to any form field in javascript in below described way:

Syntax:-

formfieldObject.readOnly=true|false

Example:-

document.getElementById("myDemoTextField").readOnly= true;


Question 26:

What is the use of typeof operator in Javascript?

The typeof operator is used to check the data type of operand in Javascript. For example:- Check whether variable is string, numeric or undefined.

For example:-

var demo = 10;
alert(typeof demo);

Click below button to test above code snippet.

Question 27:

How to open new popup window using Javascript?

You can open new popup window using open() method as shown below:

window.open("http://www.modernpathshala.com","_blank" ,"width=1000,height=1000");

To test above code click F12 on your browser and goto console tab and paste above code.

Question 28:

How will you get current date in javascript?

You can create date class object to get current date as shown in below code snippet:

var today = new Date();

For example:- Today's Date =

Question 29:

What is the difference between var x = 1 and x = 1?

Declaration 'var x = 1' will create a variable within the current scope. For example if this is declared in a function, x will not be available outside it, unless it is explicitly returned.

Declaration 'x = 1' will create a variable within the global scope. Thus, any other code on the webpage can access and change its value.

Question 30:

What is console.log()?

console.log() is used to log any message to your web console. Using firebug or developers tool, you can see the messages logged to your console using console.log().

Syntax:- 

console.log("any message goes here");
Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media