JavaScript Interview
Questions
Review these typical interview questions and think about how you would
answer them. Read the answers listed; you will find best possible answers
along with strategies and suggestions.
This page is intentionally left blank
Chapter 1
Introduction to JavaScript
1: What is JavaScript?
Answer:
JavaScript is a scripting language that adds interactivity to HTML
pages.
2: What kind of language does JavaScript provide?
Answer:
JavaScript is an interpreted language that executes scripts without
preliminary compilation.
3: Is there any connection between Java and JavaScript?
Answer:
No. They are different in every way and JavaScript is not as
powerful and complex as Java.
4: What is the official name of JavaScript and is it supported by
all browsers?
Answer:
The official name of JavaScript is ECMA (European Computer
Manufacturer's Association) and with it, Internet Explorer 4 and
Mozilla Firefox 1.5 fully supported.
5: What does JavaScript do?
Answer:
JavaScript is meant to be an easy scripting language that helps the
non-programmers with its simple syntax. JavaScript is smart
enough that it can put dynamic text into HTML pages, it can react
to events (like when a page has finished downloading), and it can
read and write HTML elements, create cookies and so forth.
6: Does prior knowledge of JAVA ease the use of JavaScript?
Answer:
Yes. Being modeled after Java, which in turn is modeled after C++,
JavaScript should be easier to familiarize and work with.
7: Is JavaScript case sensitive?
Answer:
Yes. Unlike HTML, JavaScript has to have all variables and
function names (etc.) in capital letters.
8: How do you place JavaScript?
Answer:
JavaScript may be inserted into code with the following syntax:
<script type="text/JavaScript">
9: Where do you place JavaScript?
Answer:
JavaScript may be placed in the <head> or <body> section of
HTML code, but it is usually a good practice to place it in <head>
as to not hinder your code later on.
This page is intentionally left blank
Chapter 2
Statements, Comments and
Variables
10: How do you terminate statements in JavaScript?
Answer:
In JavaScript, statements are terminated by semicolons (;) and
although they are not mandatory they are a good practice to pick
up.
11: Why are comments used in JavaScript and how are they
inserted?
Answer:
Usually comments are added to make the code more readable but
they can also be used to explain the code. They are inserted with //
(for single line comments) and /* */ for multiple lines comments.
12: What are variables and how are they inserted?
Answer:
Variables are storing containers used for holding expressions and
values. They can have a short letter or a longer name and are
inserted with the statement: var. Because the variables are loosely
typed, they can hold any type of data.
13: What does a variable of var y=10; and var catname= "Tomcat";
do?
Answer:
With the execution of the above code, we have variables that hold
values of 10(for y) and Tomcat (for catname).
Note that the inclusion of text warrants " " being used.
14: How many statements types can we find in JavaScript? Give
some examples?
Answer:
The statement types found in JavaScript are: Expression
statements, compound, empty and labeled statements.
Example: break, continue, default, do, for, etc.
15: What are conditional statements and how are they
implemented in JavaScript?
Answer:
Conditional statements are used to perform and act on different
sets of conditions declared by the programmer. They are the
following: if statement; if...else statement; if...else if...else
statement and the switch statement.
16: How will you determine a variable type in JavaScript?
Answer:
A variable type in JavaScript is determined using Typeof operator.
When the object is String, Number, Function, undefined and
Boolean, the operator returns the same type. And when the object
Example:
var count=100;
typeof count;
17: What is the dif
Answer:
is treated as String. Hence the concatenation takes place and the
18: Is it possible to assign a string to a floating point variable?
Answer:
Yes. Any variable can be assigned to another data type. For
example,
var a1=10.39;
document.write(a1); 10.39
document.write(a1); hai
19: Will variable redeclaration affect the value of that variable?
Answer:
No. The same value will be retained in the variable.
Example:
var status;
document.writ
20: How will you search a matching pattern globally in a string?
Answer:
modifier in Regular Expression.
Example:
var
Pattern_Match:First,First
21: How will you search a particular pattern in a string?
Answer:
A particular pattern in string can be searched using test function.
If the match is found it returns true, else false.
Example:
22: Which property is used to match the pattern at the start of the
string?
Answer:
ol is used for position matching.
Example:
//Pattern_Match:First First_Regular
23: Which property is used to match the pattern at the end of the
string?
Answer:
Example:
//Pattern_Match:First Expression_First
This page is intentionally left blank
Chapter 3
Operators and Functions
24: What are operators? Which are the most important operators
in JavaScript?
Answer:
Operators in JavaScript are used to combine values that form
expressions. The most important are: = and +. The first is used to
assign values and the second one is used to add values together.
25: Why comparison and logical operators are used?
Answer:
Comparison operators are used to determine if there is a
difference between variables, and also their equality, while the
logical operators are used to determine the logic of variables.
26: How many types of pop-up boxes does JavaScript have?
What are those?
Answer:
JavaScript has three types of pop-up boxes and they are: alert,
confirm and prompt.
27: Does creating an alert box prompt the user to respond with
OK or Cancel?
Answer:
No. An alert box only gives the user the option of choosing OK to
proceed.
28: What are functions in JavaScript and where are they placed?
Answer:
Functions contain code that is executed before an event thus
stopping the browser from loading a script when the page opens.
Functions can be placed both in the <head> or <body> section, but
it is advised to place them in the <head> section.