Loading

Javascript Interview Questions

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:- User can enable or disable Javascript from there browser
No support for multithreading
No support for multiprocessing
Reading and writing of files is not allowed
No support for networking applications.
4. What is the feature of Javascript?
Ans:- There are following feature of javascript:-
Javascript is an object-based scripting language
It is light weighted
JavaScript is interpreter based scripting language.
Javascript can use client side and server side both
Javascript is a functional programming language
Javascript support object.
5. What is Javascript data type?
Ans :- There are following Javascript data type:-
• Number
• String
• Boolean
• Function
• Object
• Undefined

6. What is getElementById() in Javascript?
Ans :-getElementById() is a DOM method. It return elements of 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 accept both quotes with same meaning.

8. How we can change 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 element 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 Javascript variable is case sensitive?
Ans :-Yes Javascript variable is case sensitive.

12. How to write comment 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 external Javascript file?
Ans :-
<script type=”text/javascript” src=”filename.js” ></script>

15. How to define variable in Javascript?
Ans :-Using var syntax we can define variables in Javascript. Like
<script>
var fname
var lname
</script>
we cannot use javascript reserve key word 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, undefined
one composite data type :- object

17. List the operator name which support Javascript?
Ans :- Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
typeof operator

18. Write a javascript program which explain 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 which explain if, else if conditional statement?
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 switch in Javascript?
Ans:- Switch is a conditional statement. When all condition depends upon 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 explain 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, array can be define:-
var employee = [“name1”, “name2”, “name3”, “name4”];
We can also define using array construct.
var employee = new Array(“name1”, “name2”, “name3”, “name4”);
24. In JavaScript how to convert array into string?
Ans:- toString() array method convert array to string.
var employee = [“name1”, “name2”, “name3”, “name4”];
document.getElementById(“empId”).innerHTML = employee.toString();
25. What is Join method in JavaScript?
Ans:- join() method join all array element into string.
var employee = [“name1”, “name2”, “name3”, “name4”];
document.getElementById(“empId”).innerHTML = employee.join(‘ , ‘);
26. How to remove last element of an array?
Ans:- using pop() method we can remove last element of an array.
var employee = [“name1”, “name2”, “name3”, “name4”];
employee.pop();
It will remove name4. and It return value will be name4.
27. How to add new element of an array?
Ans:- Using push() method we can add 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 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 new element at the beginning of an array?
Ans:- Using unshift() method, we can add new element at the beginning of an array. It return new array length.
var employee = [“name1”, “name2”, “name3”, “name4”];
var addedvalue = employee.unshift(“name0”);
addedvalue will return 5.
30. What is JavaScript Array length method?
Ans:- Array length property return number of elements of an array.
var employee = [“name1”, “name2”, “name3”, “name4”];
alert(employee.length);
Array length property also append new element of an array.
employee[employee.length] = name5;
31. What is the use of delete method in an array?
Ans:- “delete” method is used for delete an element of an array. It only delete an element value but not delete 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 for add and remove items from an array.
It passes two parameter, first parameter is used for how many items added and 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 array in Javascript?
Ans:- Using concat() method we can merge two or more array.
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 slice an array. It pass maximum two argument.
var empsalary = [“s1”, “s2”, “s3”];
var slemp = empsalary.slice(1)
output empsalary = [“s2”, “s3”];
35. How to convert array to string in Javascript?
Ans:- Using toString() method we can convert an array to string. It automatically convert an array to string with comma separator.
var empsalary = [“s1”, “s2”, “s3”];
document.getElementById(“test”).innerHTML = empsalary.toString();
36. How to sort an array?
Ans:-Using sort() method we can short an array. In numeric sort it give wrong result. But string sorting It always produce correct result.
37. How to short an array descending order?
Ans:-Using reverse() method we can short an array descending method.
38. How to do numeric sort in 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 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 lowest value.
Other 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 highest value
Other 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 which explain 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 call a provided function on every element of in the array and return 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 return a new array from a given array. new array consists only that element which pass 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 return single value of an array. That means it reduces an array in 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 reduceRight method in javaScript?
Ans:- reduceRight() method is similar to reduce() method. The only difference is it work Right to Left.
48. What is Javascript 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 check 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 value 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 which satisfy given condition. It check all element of an array and print first element of an array which 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 find first index of an array, which satisfy given condition.
var checkval = [10,30,20,15,25];
function testchek(value){
return value>20;
}
checkval.findIndex(testchek);
}

Share with:


4+

Leave a Reply

Connect with:




z35W7z4v9z8w