Java Script

Interview Questions on Java Script

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

var fruits = ["Banana", "Orange", "Apple", "Mango"];
// 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?


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);