Interview Questions on Java Script

61. How to sort the elements in alphabetical order in descending?

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();
// fruits contains : Orange,Mango,Banana,Apple

62. What is the difference b/w array and objects?

we can access array values with number.
We can access objects values with key values.

63. Which is the best way to create an array? a. var points = new Array(); b. var points = [];

Answer is b.

64. What is the type of an array?

Object

65. How do I know if a variable is an array?

Use this function 
function isArray(myArray) {
    return myArray.constructor.toString().indexOf("Array") > -1;
}

66. How to remove the last element in an array?

The pop() method removes the last element from an array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();              // Removes the last element ("Mango") from fruits

67. How to remove the first element in an array?

the shift() method removes the first element of an array.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift();  // Removes the first element from fruits

68. How to add the element to the last in an array?

The push() method adds a new element to an array (at the end):
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");       //  Adds a new element ("Kiwi") to fruits

69. How to disable right click in javascript?

Use code :-

70. How to submit a form without mouse click on submit button?

Use :    document.forms["formname"].submit();

71. How to redirect to another page?

Use :-  window.location="url_name";

72. How to execute function periodically?

setInterval(function () {   
      //call functions here     
}, 3000);

Questions 61 - 72

Interview Questions on Java Script

51. What is the use of substr()?

The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.
var str = "Hello world!";
var res = str.substring(1, 4);
res contains: ell

52. What is the use of toLowerCase()?

Converts all characters in string into lowercase.

53. What is the use of toUpperCase()?

Converts all characters in string into uppercase.

54. What is the use Of toString()?

Converts a value to string datatype.

55. How to remove extra white space?

Use trim().
Ex :- 
var str = "  Hello world!  ";
var res = str.trim();
res contains: Hello world!

56. How to convert string to float?

Float.parseFloat(“value”);
Ex:- 
var str =”3.5”;
var val = Float.parseFloat(str);

57. How to convert string to int?

Integer.parseInt(“value”);
Ex:- 
var str =”45”;
var val = Integer.parseInt(str);

58. How to create an array and access the value of an array?

Declare :var array1 = [2,5,7,3,5];
Access : var value1 = array1[0];

59. How to find the size of an array?

Syntax :- arrayname.legth;
Ex:- 
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var legth = fruits.length;
length contains 4.

60. How to sort the elements in alphabetical order in ascending?

Syntax : - arrayname.sort();
Ex:- 
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
// fruits contains : Apple,Banana,Mango,Orange

Questions 51 - 60

Interview Questions on Java Script

41. How to write “ in string that is enclosed with in doublequotes?

Use \”

42. How to write new line in string?

Use \n

43. How to write string in multiples lines?

Use \

44. Can we compare objects?

You can compare keys of objects.

45. How to get character from particular position in string?

Use CharAt(val);
Ex :- 
var str = "HELLO WORLD";
var res = str.charAt(0);

46. What is the method used for join two strings?

Conacat();
Ex :-
var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);

47. What is the use of indexOf()?

Tells position of particular character or string.
Ex:-
var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");

48. What is the use of lastIndexOf()?

Tells position of last occurrence of  particular character or string.
Ex:-
var str = "Hello world, welcome to the universe.";
var n = str.lastIndexOf("welcome");

49. How to replace part of string?

Use str.replace();
Ex :-
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");

50. What is the use of split()?

The split() method is used to split a string into an array of substrings, and returns the new array.
Ex :-
var str = "How are you doing today?";
var res = str.split(" ");
res contains: How,are,you,doing,today?

Questions 41 - 50

Interview Questions on Java Script

31. What is function in javascript?

Def :- Set of statements to perform a particular operation.
Syntax :- 
function function_name(arg1,arg2){
//body
}
Ex :- 
function myFunction(p1, p2) {
    return p1 * p2;   
    // The function returns the product of p1 and p2
}

32. Can we access local variable out side the function?

No. they are permitted to that function only.

33. How to create global variables?

Create variables outside function.

34. How to execute javascript function when particular button clicked?

Use onclick attribute.
Ex:- <button onclick=”functionname”> Button1 </buttuon>.

35. How to execute javascript function when form is submitted?

Use onsubmit attribute.
Ex:- <button onsubmit=”functionname”> OK </buttuon>.

36. How to execute function when mouse over on hyperlik?

use onmouseover attribute. 
<a href='index.html' 
    onmouseover='this.style.textDecoration="none"' 
    onmouseout='this.style.textDecoration="underline"'>
    Click Me
</a>

37. How to execute function when mouse leaves on hyperlik?

use onmouseout attribute. 
<a href='index.html' 
    onmouseover='this.style.textDecoration="none"' 
    onmouseout='this.style.textDecoration="underline"'>
    Click Me
</a>

38. How to execute function select option from dropdown list?

Use onchange attribute.
Ex :-
<select id="leave" onchange="changeMessage()" >
  <option value="5">Get Married</option>
  <option value="100">Have a Baby</option>
  <option value="90">Adopt a Child</option>
</select>

39. Create vehicle object which has type, model and color values?

var car = {type:"Fiat", model:500, color:"white"};

40. How to access members of object variable?

Syntax :-  objectname.keyname
Ex:- var color = car.color;
41. What are the uses of javascript?
1. For client side validations.
2. Tomake interactive effects within the browser.
3. To make request and load response in div without loading entire page.

Questions 31 - 40

Interview Questions on Java Script

21. How to convert number to string?

var num = 15;
var n = num.toString();

22. What is the difference b/w == and ===?

==  compares the values.
=== compare values and its type.

23. How to create an array in javascript?

Method1 :- var cars = ["Saab", "Volvo", "BMW"];
Method2 :-  var cars = new Array("Saab", "Volvo", "BMW");

24. How to declare and initialize object?

 Syn :- var objectname = {keyname1:vaule1, keyname2:value2};
 Ex :- var person = {firstName:"John", lastName:"Doe", age:46};

25. How to know the type of vaue of variable?

you can use the JavaScript typeof operator to find the type of a JavaScript variable
Ex :- 
typeof "John"                // Returns string 
typeof 3.14                  // Returns number
typeof false                 // Returns boolean
typeof [1,2,3,4]             // Returns object
typeof {name:'John', age:34} // Returns object

26. If we try to print variable which is not initialize what will be the output?

undefined

27. What is the type of null variable?

Object

28. What is the type of undefined variable?

undefined

29. What is the difference b/w undefined and null?

Type of undefined is undefined.
Type of null is object.

30. What is the difference b/w empty and null?

Empty is a value.
Null is not a value.

Questions 21 - 30

Interview Questions on Java Script

11. How to append the content to particular div element?

document.getElementById(‘#idname).innerHTML += ‘html code goes here’ ;

12. How to get value of input box in javascript?

document.getElementById(‘#idname).value;

13. How to declare and initialize variables?

Syntax :- var van_name = value;
Ex:-  var var1 = 5;

14. How to concat two strings in javascript?

By using +
Ex:- var var = “string1”+”string2”;

Ex 2:- 
var var1 = ”string1”;
var var2 = “string2”; 
var var3 = var1 + var2;

15. How to write comments in javascript?

Method1 :-  // comments goes here
Method2 :-  /* comments goes here */

16. What are the rule to write an identifier?

The general rules for constructing names for variables (unique identifiers) are:
1. Names can contain letters, digits, underscores, and dollar signs.
2. Names must begin with a letter
3. Names can also begin with $ and _ (but we will not use it in this tutorial)
4. Names are case sensitive (y and Y are different variables)
5. Reserved words (like JavaScript keywords) cannot be used as names

17. Are Javascript Identifiers case sensitive?

Yes

18. What are the ways to write multiple words as variablename?

1. Use _ b/w words.
2. Use camelcase(capital letter for every first letter).

19. Tell me some of the javascript keywords?

var,if, while,for, break,continue,function,new

20. How to convert string to number?

Integer.parseInt(“string value”);


Questions 11 - 20

Interview Questions on Java Script

1. What is Java script?

Java Script is a programming language that is used to create interactive effects within web browsers.

2. How to write java script in html page?

By using  

3. Where can we write javascript code in HTML?

Any where. Whithin body or div or head.

4. How to link externally the javascript file?

syntax:-
<script type="text/javascript" src="external_javascript.js"></script>
Ex:-
<script type="text/javascript" src="validator.js"></script>

5. Write java script code to popup alertbox?

Bay using alert();
Ex:- alert(‘alert text goes here’);

6. How to write content to the console?

By using console.log();
Ex:- console.log(‘write text here’);

7. How to write content to the fresh document?

By using document.write();
Ex:- document.write(‘write content here’);

8. How to change the styles of html element in javascript?

Syntax :- document.getElementById(‘idname’).style.propertynname=’value’;
Ex : - document.getElementById(‘container).style.color=red; 

9. How to get content of particular div element?

By using innerHTML property
Ex:- var1 = document.getElementById(‘#container’).innerHTML;

10. How to change the content of particular div element?

document.getElementById(‘#idname).innerHTML = ‘html code goes here’ ;

Questions 01 - 10