Latest JavaScript Interview Questions & Answer for 2024
JavaScript is a versatile, high-level programming language commonly used to create interactive effects within web browsers. As a core technology of the World Wide Web alongside HTML and CSS, JavaScript enables developers to implement complex features on web pages, such as dynamic content updates, interactive forms, animations, and more. Its flexibility and wide range of libraries and frameworks, like React, Angular, and Node.js, make it a powerful tool for both front-end and back-end development. JavaScript is known for its event-driven, functional, and imperative programming styles, making it a favorite among developers for creating responsive and engaging web applications.
Here are some interview questions :
1) Which type of JavaScript language is ___
a.Object-Oriented
b.Object-Based
c.Assembly-language
d.High-level
Answer: B
2. What is JavaScript?
a. JavaScript is a scripting language used to make the website interactive
b. JavaScript is an assembly language used to make the website interactive
c. JavaScript is a compiled language used to make the website interactive
d. None of the mentioned
Answer: a
3. The ” var” and “function” are known as _____________.
a. Data types
b. Keywords
c. Prototypes
d. Declaration statements
Answer: (d) Declaration statements
4. Which of the following is correct about JavaScript?
a) JavaScript is an Object-Based language
b) JavaScript is Assembly-language
c) JavaScript is an Object-Oriented language
d) JavaScript is a High-level language
Answer: a
5) Which of the following is the correct output for the following JavaScript code:
varx=5,y=1
var obj ={ x:10}
with(obj)
{
alert(y)
}
a.1
b.Error
c.10
d.5
Answer: A
6. Which of these is the correct way in which we can call the JavaScript code?
a. Triggering Event
b. Preprocessor
c. Function/Method
d. RMI
Answer: (c) Function/Method
7. Among the given statements, which statement defines closures in JavaScript?
a. JavaScript is a function that is enclosed with references to its inner function scope
b. JavaScript is a function that is enclosed with references to its lexical environment
c. JavaScript is a function that is enclosed with the object to its inner function scope
d. None of the mentioned
Answer: b
8) Which one of the following also known as Conditional Expression:
a.Alternative to if-else
b.Switch statement
c.If-then-else statement
d.immediate if
Answer: D
9. Which of these functions of the Number Object would format a number with different numbers of digits to the decimal’s right?
a. toFixed()
b. toExponential()
c. toLocaleString()
d. toPrecision()
Answer: (a) toFixed()
10. What will be the output of the following JavaScript code snippet?
<p id="demo"></p>
var txt1 ="Sanfoundry_";
var txt2 ="Javascriptmcq";
document.getElementById("demo").innerHTML= txt1 + txt2;
a) error
b) Sanfoundry_ Javascriptmcq
c) undefined
d) Sanfoundry_Javascriptmcq
Answer: d
11) Among the following given JavaScript snipped codes, which is more efficient:
Code A
for(var number=10;number>=1;number--)
{
document.writeln(number);
}
Code B
var number=10;
while(number>=1)
{
document.writeln(number);
number++;
}
a.Code 1
b.Code 2
c.Both Code 1 and Code 2
d.Cannot Compare
Answer: A
12. Out of the following functions of the string object, which one would return the character in any string via the specified number of characters starting at a specified position?
a. search()
b. substr()
c. split()
d. slice()
Answer: (b) substr()
13. What will be the output of the following JavaScript code?
<p id="demo"></p>
<script>
var js =10;
js *=5;
document.getElementById("demo").innerHTML= js;
</script>
a) 10
b) 50
c) 5
d) Error
Answer: b
14) In JavaScript, what is a block of statement?
a.Conditional block
b.block that combines a number of statements into a single compound statement
c.both conditional block and a single statement
d.block that contains a single statement
Answer: B
15. Look at the snippets given below and check the one in which the variable “a” isn’t equal to the “NULL”.
a. if (a!)
b. if(a!=null)
c. if(a!==null)
d. if(a!null)
Answer: (c) if(a!==null)
16. Arrays in JavaScript are defined by which of the following statements?
a) It is an ordered list of values
b) It is an ordered list of objects
c) It is an ordered list of string
d) It is an ordered list of functions
Answer: a
17) When interpreter encounters an empty statements, what it will do:
a.Shows a warning
b.Prompts to complete the statement
c.Throws an error
d.Ignores the statements
Answer: D
18. In JavaScript, what do we use for calling the expression for function definition?
a. Function literal
b. Function prototype
c. Function declaration
d. Function calling
Answer: (a) Function literal
19. What will be the output of the following JavaScript code?
// JavaScript Comparison Operators
function compare()
{
let a=2;
let b=2.0;
if(a==b)
returntrue;
else
returnfalse;
}
a) false
b) true
c) compilation error
d) runtime error
Answer: b
20) The "function" and " var" are known as:
a.Keywords
b.Data types
c.Declaration statements
d.Prototypes
Answer: C
21. Which of these is used in JavaScript for calling a method or a function?
a. Functional Expression
b. Property Access Expression
c. Primary Expression
d. Invocation Expression
Answer: (d) Invocation Expression
22. What will be the output of the following JavaScript code snippet?
// JavaScript Equalto Operators
function equalto()
{
let num=10;
if(num==="10")
returntrue;
else
returnfalse;
}
a) false
b) true
c) compilation error
d) runtime error
Answer: a
23) In the following given syntax of the switch statement, the Expression is compared with the labels using which one of the following operators?
switch(expression)
{
statements
}
a.===
b.equals
c.==
d.Equals
Answer: A
24. Which of these operators are used for checking if a specific property exists?
a. in
b. within
c. exist
d. exists
Answer: (a) in
25. Will the following JavaScript code work?
var js =(function(x){return x*x;}(10));
a) Exception will be thrown
b) Memory leak
c) Error
d) Yes, perfectly
Answer: d
26) What will happen, if the following JavaScript code is executed?
var count =0;
while (count <10)
{
console.log(count);
count++;
}
a.An error is displayed
b.An exception is thrown
c.The values of count variable are logged or stored in a particular location or storage
d.The value of count from 0 to 9 is displayed in the console
Answer: C
27. “The expression that can appear legally on an assignment expression’s left side” is a common explanation for variables, elements of arrays, and properties of objects. These are known as __________:
a. Prototypes
b. Properties
c. Lvalue
d. Definition
Answer: (c) Lvalue
28. Which of the following is not javascript data types?
a) Null type
b) Undefined type
c) Number type
d) All of the mentioned
Answer: d
29) Which of the following is the correct output for the following JavaScript code:
Int x=8;
if(x>9)
{
document.write(9);
}
else
{
document.write(x);
}
a.9
b.0
c.8
d.Undefined
Answer: C
30. Which of these is a correct output for the JavaScript code given below?
string X= “Hey”;
string Y=”There”;
alert(X+Y);
a. Hey There
b. Hey_There
c. HeyThere
d. undefined
Answer: (c) HeyThere
31. Where is Client-side JavaScript code is embedded within HTML documents?
a) A URL that uses the special javascript:code
b) A URL that uses the special javascript:protocol
c) A URL that uses the special javascript:encoding
d) A URL that uses the special javascript:stack
Answer: b
32) Which of the following is the correct output for the following JavaScript code:
var grade='C';
var result;
switch(grade)
{
case'A':
{
result+="10";
break;
}
case'B':
{
result+=" 9";
break;
}
case'C':
{
result+=" 8";
break;
}
default:
result+=" 0";
}
document.write(result);
-
10
-
9
-
8
-
0
Answer: C
33. Which of these is known as the Equality operator used for checking whether both the values are equal?
a. =
b. ==
c. ===
d. &&
Answer: (b) ==
34. What will be the output of the following JavaScript code snippet?
int a=1;
if(a!=null)// JavaScript not equal to Operators
return1;
else
return0;
a) 0
b) 1
c) compiler error
d) runtime error
Answer: b
35) Which of the following is the correct output for the following JavaScript code:
var grade='D';
var result;
switch(grade)
{
case'A':
result+="10";
case'B':
result+=" 9";
case'C':
result+=" 8";
case 'D'
result+=" 6";
default:
result+=" 0";
}
document.write(result);
a.10
b.6
c.33
d.0
Answer: B
36. In case a value of an operator is NULL, then the unary operator would return the ____________ typeof.
a. object
b. boolean
c. string
d. undefined
Answer: (d) undefined
37. Which of the following object is the main entry point to all client-side JavaScript features and APIs?
a) Position
b) Window
c) Standard
d) Location
Answer: b
38) Which of the following is the correct output for the following JavaScript code:
var x=3;
var y=2;
var z=0;
If(x==y)
document.write(x);
elseif(x==y)
document.write(x);
else
document.write(z);
a.3
b.0
c.Error
d.2
Answer: B
39. Which of these is not a keyword?
a. debugger
b. use strict
c. with
d. if
Answer: (b) use strict
40. Which of these symbols is used to create comments in JavaScript?
a. //
b. \\
c. \* */
d. \* *\
Answer: (a) //
41. In the line of code given below, what will the “datatype” written in brackets be called?
article[datatype]=assignment_value;
a. An object
b. A String
c. Floating point
d. An integer
Answer: (b) A String
42. What will be the output of the following JavaScript program?
function sanfoundry(javascript)
{
return(javascript ? “yes” : “no”);
}
bool ans=true;
console.log(sanfoundry(ans));
a) Compilation error
b) Runtime error
c) Yes
d) No
Answer: c
43. What will be the output of the following JavaScript code?
// Javascript code snippet to compare the height
function height()
{
var height =123.56;
var type =(height>=190)?"tall":"short";
return type;
}
a) short
b) 123.56
c) tall
d) 190
Answer: a
44) Which of the following is the correct output for the following JavaScript code:
var grade='Z';
var result;
switch(grade)
{
case'A':
result+="10";
case'B':
result+=" 9";
case'C':
result+=" 8";
default:
result+=" 0";
}
document.write(result);
-
10
-
17
-
18
-
0
Answer: D
45. Which of these codes is equivalent to the code given below?
a.x(g,h);
a. a [ “x” ] ( g , h );
b. x (g) &&a.x (h);
c. x( g&&h );
d. a (x )[ “g” , “h” ];
Answer: (a) a [ “x” ] ( g , h );
46. Which of these keywords is used to define various functions in JavaScript?
a. function
b. main
c. init
d. Void
Answer: (a) function
47) Which one of the following is the correct way for calling the JavaScript code?
-
Preprocessor
-
Triggering Event
-
RMI
-
Function/Method
Answer: D
48. What will be the output of the following JavaScript function?
<p id="demo"></p>
<script>
function javascript()
{
// javacript abs() method
document.getElementById("demo").innerHTML= Math.abs(-7.25);
}
</script>
a) -7.25
b) 7.25
c) -7
d) 7
Answer: b
49. Which of the following scoping type does JavaScript use?
a) Sequential
b) Segmental
c) Lexical
d) Literal
Answer: c
50. What is the basic difference between JavaScript and Java?
a) Functions are considered as fields
b) Functions are values, and there is no hard distinction between methods and fields
c) Variables are specific
d) There is no difference
Answer: b
Categories: web designing web development javascript