JavaScript Interview Questions(5)

潘志国
2023-12-01

1.What are the pop-up boxes available in JavaScript?

  • Alert Box
  • Confirm Box
  • Prompt Box

Example of alert() in JavaScript

<script type="text/javascript">  
function msg(){  
 alert("Hello Alert Box");  
}  
</script>  
<input type="button" value="click" onclick="msg()"/>  

Example of confirm() in JavaScript

<script type="text/javascript">  
function msg(){  
var v= confirm("Are u sure?");  
if(v==true){  
alert("ok");  
}  
else{  
alert("cancel");  
}  
  
}  
</script>  
  
<input type="button" value="delete record" onclick="msg()"/>  

Example of confirm() in JavaScript

<script type="text/javascript">  
function msg(){  
var v= confirm("Are u sure?");  
if(v==true){  
alert("ok");  
}  
else{  
alert("cancel");  
}  
  
}  
</script>  
  
<input type="button" value="delete record" onclick="msg()"/>  

Example of prompt() in JavaScript

<script type="text/javascript">  
function msg(){  
var v= prompt("Who are you?");  
alert("I am "+v);  
  
}  
</script>  
  
<input type="button" value="click" onclick="msg()"/>  

2.How can we detect OS of the client machine using JavaScript?

The navigator.appVersion string can be used to detect the operating system on the client machine.

3.How to submit a form using JavaScript by clicking a link?

<script type="text/javascript">  
document.body.bgColor="pink";  
</script>  

4.How to change the background color of HTML document using JavaScript?

<script type="text/javascript">  
document.body.bgColor="pink";  
</script>  

5.How to handle exceptions in JavaScript?

  • By the help of try/catch block, we can handle exceptions in JavaScript.
  • JavaScript supports try, catch, finally and throw keywords for exception handling.

6. How to validate a form in JavaScript?

<script>  
function validateform(){  
var name=document.myform.name.value;  
var password=document.myform.password.value;  
  
if (name==null || name==""){  
  alert("Name can't be blank");  
  return false;  
}else if(password.length<6){  
  alert("Password must be at least 6 characters long.");  
  return false;  
  }  
}  
</script>  
<body>  
<form name="myform" method="post" action="abc.jsp" onsubmit="return validateform()" >  
Name: <input type="text" name="name"><br/>  
Password: <input type="password" name="password"><br/>  
<input type="submit" value="register">  
</form>  

7.How to validate email in JavaScript?

There are many criteria that need to be follow to validate the email id such as:

  • email id must contain the @ and . character
  • There must be at least one character before and after the @.
  • There must be at least two characters after . (dot).
<script>  
function validateemail()  
{  
var x=document.myform.email.value;  
var atposition=x.indexOf("@");  
var dotposition=x.lastIndexOf(".");  
if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){  
  alert("Please enter a valid e-mail address \n atpostion:"+atposition+"\n dotposition:"+dotposition);  
  return false;  
  }  
}  
</script>  
<body>  
<form name="myform"  method="post" action="#" onsubmit="return validateemail();">  
Email: <input type="text" name="email"><br/>  
  
<input type="submit" value="register">  
</form>  

8.What is this keyword in JavaScript?

  • The this keyword is a reference variable that refers to the current object.
var address=    
{    
company:"Javatpoint",    
city:"Noida",    
state:"UP",    
fullAddress:function()    
{    
return this.company+" "+this.city+" "+this.state;    
}    
};    
var fetch=address.fullAddress();    
document.writeln(fetch);    

9.What is the requirement of debugging in JavaScript?

  • JavaScript didn’t show any error message in a browser.

  • However, these mistakes can affect the output.

  • The best practice to find out the error is to debug the code.

  • The code can be debugged easily by using web browsers like Google Chrome, Mozilla Firebox.
    To perform debugging, we can use any of the following approaches:

  • Using console.log() method

  • Using debugger keyword

10.What is the use of debugger keyword in JavaScript?

  • JavaScript debugger keyword sets the breakpoint through the code itself.
  • The debugger stops the execution of the program at the position it is applied.
  • we can start the flow of execution manually.
  • If an exception occurs, the execution will stop again on that particular line…
function display()  
{  
x = 10;    
y = 15;    
z = x + y;    
debugger;    
document.write(z);    
document.write(a);     
}     
display();  
 类似资料: