Javascript Interview Questions Answers
Javascript Interview Questions Answers for beginners and professionals with a list of top frequently asked in Javascript which include from basic to advance
1. What is Javascript?
Ans:- Javascript is a client-side scripting language. It is different from Java. The JavaScript Translator is responsible for translating the JavaScript code for the web browser.
2. What is the advantage of Javascript?
Ans:- Advantage of Javascript
Speed
Simplicity
Popularity
Interoperability
Server Load
3. What is the disadvantage of Javascript?
Ans:- Disadvantage of Javascript:-
Security:- Users can enable or disable Javascript from their browser
No support for multithreading
No support for multiprocessing
Reading and writing of files are not allowed
No support for networking applications.
4. What is the feature of Javascript?
Ans:- There are the following feature of javascript:-
Javascript is an object-based scripting language
It is light weighted
JavaScript is an interpreter-based scripting language.
Javascript can use the client side and server side both
Javascript is a functional programming language
Javascript support object.
5. What is the Javascript data type?
Ans:- There are the following Javascript data type:-
• Number
• String
• Boolean
• Function
• Object
• Undefined
6. What is getElementById() in Javascript?
Ans:-getElementById() is a DOM method. It returns elements of a specific ID.
document.getElementById(“test”).innerHTML = “ID of Javascript”;
< p id=”test” > change the content.< /p >
< button type=”button” onclick=’document.getElementById(“test”).innerHTML = “ID of JavaScript!”‘ >Click Here!< /button >
7. Is Javascript accept double and Single Quotes?
Ans:- Javascript accepts both quotes with the same meaning.
8. How we can change the HTML attribute value using Javascript?
Ans:- using document.getElementById we can change the attribute value.
document.getElementById(“test”).style.color=’red’
9. How to show and hide HTML elements using Javascript?
Ans:- For hide
document.getElementById(“test”).style.display=’none’
For show
document.getElementById(“test”).style.display=’block
10. How to insert Javascript code into HTML?
Ans :- using <script >Javascript code< /script>
< script language=”javascript” type=”text/javascript” >
< !– Javascript code //– >
< /script>
11. Is the Javascript variable case-sensitive?
Ans:-Yes the Javascript variable is case sensitive.
12. How to write comments in Javascript?
Ans:-
Single line comment //
Multiline comment /* */
HTML type comment < !– //–>
13. How to check browser support Javascript?
Ans:-using < noscript>
14. How to include an external Javascript file?
Ans:-
<script type=”text/javascript” src=”filename.js” ></script>
15. How to define variables in Javascript?
Ans:-Using var syntax we can define variables in Javascript. Like
<script>
var fname
var lname
</script>
we cannot use javascript reserve keywords like abstract, long, native, this, class, etc. as a variable name.
16. Which data type support Javascript?
Ans:- Javascript support three primitive data type:- Numbers, string, and boolean
and two trivial data type:- null, and undefined
one composite data type:- object
17. List the operator name which supports Javascript?
Ans :- Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
typeof operator
18. Write a javascript program that explains if, else conditional statement?
Ans :-<script type=”text/javascript”>
<!–
var salary = 15000;
if( salary > 18000 ){
document.write(“salary is good“);
}else{
document.write(“required increment.“);
}
//–>
</script>
19. Write a javascript program that explains if, else if conditional statements?
Ans:- <script type=”text/javascript”>
<!–
var color = “red”;
if( book == “green” ){
document.write(“Green color“);
}
else if( book == “red” ){
document.write(“Red color“);
}
else if( book == “brown” ){
document.write(“Brown color“);
}
else{
document.write(“color not defined“);
}
//–>
</script>
20. What is the switch in Javascript?
Ans:- Switch is a conditional statement. When all condition depends upon a single variable at that case we can use a switch statement. Like
switch(expression){
case condition1:
statements
break;
case condition 2:
statements
break;
———
———
case condition n:
statements
break;
default;
statements
}
21. Write a code Which explains Javascript switch-case statement?
Ans:- <html>
<body>
<script type=”text/javascript”>
<!–
var color=’Red’;
document.write(“Entering switch block”);
switch (color)
{
case ‘Red’: document.write(“Color is Red.”);
break;
case ‘Blue’: document.write(“Color is blue.”);
break;
case ‘Yellow’: document.write(“Color is Yellow.”);
break;
default: document.write(“Color is white”)
}
document.write(“Exiting switch block”);
//–>
</script>
</body>
</html>
22. Who develop JavaScript?
Ans:- JavaScript develop by Brendan Eich in the year 1995, Which first time appear that time popular browser Netscape.
23. What is an array and how array define in JavaScript?
Ans:- Array is an object which can store a collection of items. If we want to store a large amount of data in the same category then the array will be very useful.
In JavaScript, an array can be defined as:-
var employee = [“name1”, “name2”, “name3”, “name4”];
We can also define using an array construct.
var employee = new Array(“name1”, “name2”, “name3”, “name4”);
24. In JavaScript how to convert the array into a string?
Ans:- toString() array method converts the array to string.
var employee = [“name1”, “name2”, “name3”, “name4”];
document.getElementById(“empId”).innerHTML = employee.toString();
25. What is the Join method in JavaScript?
Ans:- join() method join all array element into a string.
var employee = [“name1”, “name2”, “name3”, “name4”];
document.getElementById(“empId”).innerHTML = employee.join(‘ , ‘);
26. How to remove the last element of an array?
Ans:- using the pop() method we can remove the last element of an array.
var employee = [“name1”, “name2”, “name3”, “name4”];
employee.pop();
It will remove name4. and Its return value will be name4.
27. How to add a new element to an array?
Ans:- Using the push() method we can add a new element of an array.
var employee = [“name1”, “name2”, “name3”, “name4”];
var addedvalue = employee.pop(“name5”);
addedvalue will return 5.
28. How to extract an element from the beginning of an array?
Ans:- Using method shift() we can extract the first element of an array. All rest of the elements to a lower index.
shift() return shifted out value.
var employee = [“name1”, “name2”, “name3”, “name4”];
var addedvalue = employee.shift(“name5”);
addedvalue will return 1.
29. How to add a new element at the beginning of an array?
Ans:- Using unshift() method, we can add a new element at the beginning of an array. It returns a new array length.
var employee = [“name1”, “name2”, “name3”, “name4”];
var addedvalue = employee.unshift(“name0”);
addedvalue will return 5.
30. What is the JavaScript Array length method?
Ans:- Array length property returns the number of elements of an array.
var employee = [“name1”, “name2”, “name3”, “name4”];
alert(employee.length);
Array length property also appends a new element of an array.
employee[employee.length] = name5;
31. What is the use of the delete method in an array?
Ans:- “delete” method is used to delete an element of an array. It only deletes an element value but does not delete the key.
var employee = [“name1”, “name2”, “name3”, “name4”];
delete employee[0]; // It will delete “name1”
alert(employee.length); // It will return 4
32.Tell about splice() method in JavaScript?
Ans:- splice() method is used to add and remove items from an array.
It passes two parameters, the first parameter is used for how many items are added and the second parameter is used for how many items will remove.
var employee = [“name1”, “name2”, “name3”, “name4”];
employee.splice(1, 2, name5);
output Array :- [“name1”, “name2”, “name5”]
33. How to merge two or more arrays in Javascript?
Ans:- Using the concat() method we can merge two or more arrays.
var empname = [“n1”, “n2”, “n3”];
var empsalary = [“s1”, “s2”, “s3”];
var empage =[“a1”, “a2”, “a3”];
var empdetail = empname.concat(empsalary, empage);
34. Tell about slice() method?
Ans:- Slice method slices an array. It passes a maximum of two arguments.
var empsalary = [“s1”, “s2”, “s3”];
var slemp = empsalary.slice(1)
output empsalary = [“s2”, “s3”];
35. How to convert an array to a string in Javascript?
Ans:- Using the toString() method we can convert an array to a string. It automatically converts an array to a string with a comma separator.
var empsalary = [“s1”, “s2”, “s3”];
document.getElementById(“test”).innerHTML = empsalary.toString();
36. How to sort an array?
Ans:-Using the sort() method we can sort an array. In numeric sort, it gives the wrong result. But string sorting always produces the correct result.
37. How to sort an array in descending order?
Ans:-Using the reverse() method we can short an array descending method.
38. How to do numeric sort in a Javascript array?
Ans:-Using compare function we can do numeric sort.
var numArray = [10,50,20,40,30];
numArray.sort(function(x,y){return y-x});
39. How to sort an array randomly?
Ans:-var numArray = [10,50,20,40,30];
numArray.sort(function(x,y){return 0.5 – Math.random()});
40. How to find the highest and lowest value of an array?
Ans:-var numArray = [10,50,20,40,30];
numArray.sort(function(x,y){return x-y});
numArray[0]
It will return the lowest value.
Another method is Math. min(numArray)
var numArray = [10,50,20,40,30];
numArray.sort(function(x,y){return y-x});
numArray[0]
It will return the highest value
Another method is Math. max(numArray)
41. How to short an object in Javascript?
Ans:-Using compare method we can sort an object in Javascript.
var school = [
{type: “student”, total:100},
{type: “teacher”, total:10},
{type: “staff”, total:3}
];
school.sort(function(x, y){return x.total – y.total});
For string sorting
school.sort(function(x, y){
var a = x.type.toLowerCase();
var b = y.type.toLowerCase();
if(a < b){return -1;}
if(a>b){return 1;}
return 0;
)};
42. How to use Loop in Javascript?
Ans:-Javascript support
for loop
for/in loop
while loop
do/ while loop
43. Write an example that explains JavaScript foreach iteration?
Ans:- foreach() function execute a provided function once for each array element.
var i = ”;
var name = [apple, orange, coconut, banana, mango];
name.forEach(nameFunctio);
function nameFunction(value, index, array){
i = i + value + “
“;
}
44. What is the Javascript map() method?
Ans:- map() method calls a provided function on every element of the array and returns the new array in the same size.
var num = [2, 4, 6, 8, 10];
num.map(numFunctio);
function numFunction(value, index, array){
return value * 3;
}
map() method is useful for changing or altering data on in an array.
45. What is the filter() method in Javascript?
Ans:- A FILTER() method returns a new array from a given array. the new array consists only of that element that passes certain test criteria of a function.
Example:-
function checkNumber(){
return value > 3;
}
var filtCheck = [1,2,3,4,5,6,7,8,9] . filter(checkNumber);
print filtCheck;
46. What is the reduce method in Javascript?
Ans:- Javascript reduce() method returns a single value of an array. That means it reduces an array to a single value. It works left to right.
var sumarra = [2,4,6,8];
var sum = sumarra.reduce((acc,value) =>{
return acc + value;
} );
47. What is the reduceRight method in JavaScript?
Ans:- reduceRight() method is similar to reduce() method. The only difference is it works Right to Left.
48. What is Javascript’s every() method?
Ans:- every() method navigate all elements of an array and test it to pass certain criteria.
var checkval = [10,30,20,15,25];
function testchek(value){
return value>23;
}
checkval.every(testchek);
49. What is some() method in javascript?
Ans:- some() method navigate an array and checks some value pass the test;
var checkval = [10,30,20,15,25];
function testchek(value){
return value>23;
}
checkval.some(testchek);
50. What is the indexOf() method in Javascript?
Ans:- IndexOf() method navigate an array. and search certain values and return this position of particular value.
It starts the search from the start of the value.
lastindexOf() method search from the end of an array value.
51. What is found () method in Javascript?
Ans:- find method find the first element of an array that satisfies a given condition. It checks all element of an array and prints the first element of an array that satisfy the condition.
var checkval = [10,30,20,15,25];
function testchek(value){
return value>20;
}
checkval.find(testchek);
}
52. What is findIndex() method in Javascript?
Ans:- findIndx() method finds the first index of an array, which satisfies the given condition.
var checkval = [10,30,20,15,25];
function testchek(value){
return value>20;
}
checkval.findIndex(testchek);
}
Read Also:-
AJAX Interview Questions Answers
In Wikipedia Learn more about javascript
One thought on “Javascript Interview Questions”