JavaScript

1. Java Script Module 2. World Wide Web (WWW) 3. Web Terms 4. NEED FOR SCRIPT 5. Introduction to Internet Programming 6. CLIENT SIDE and SERVER SIDE Scripting 7. Different Scripting Languages 8. Introduction to JavaScript 9. JavaScript and Security 10. How to Enable JavaScript 11. Advantages of Java Script 12. Limitations with JavaScript 13. Difference between JavaScript and JAVA 14. Difference between JavaScript and VBScript 15. JavaScript Development Tools 16. JavaScript Programming Techniques 17. JavaScript terms 18. Starting with JavaScript Programming step by step 19. Time to start JavaScript Programming 20. JavaScript Variables 21. JavaScript RESERVED WORDS 22. JavaScript Data Type 23. NUMBER DATA TYPE 24. BOOLEAN DATA TYPE 25. STRING DATA TYPE 26. NULL DATA TYPE 27. JavaScript Undefined DATA TYPE 28. JavaScript Operators 29. JavaScript Arithmetic Operators 30. JavaScript Logical Operators 31. JavaScript Comparison Operators 32. JavaScript String Operators 33. Bit Manipulation Operators 34. JavaScript Assignment Operators 35. JavaScript Conditional Operator 36. Conversion between JavaScript Data Types 37. Alert JavaScript function 38. confirm JavaScript function 39. prompt JavaScript function 40. JavaScript Statements 41. Single line & Multi-line Comments 42. JavaScrip If Statement 43. JavaScript if..else Statements 44. JavaScript If…else..if statement. 45. JavaScript For Statement 46. JavaScript For..in Statement: 47. JavaScript While Statement 48. JavaScript do while Statement: 49. JavaScript break and continue statement 50. JavaScript switch Statement: 51. JavaScript function() 52. JavaScript function with a Return Value 53. JavaScript with Statement 54. try, catch, and throw Statements 55. JavaScript ARRAY 56. JavaScript array of different data types 57. JavaScript ARRAY and Bubble sorting Program 58. JavaScript Array Methods 59. concate() method 60. every() method 61. filter() method 62. forEach() method 63. indexOf() method 64. Join() method 65. lastIndexOf() method 66. map() method 67. reduce() method 68. reduceRight() method 69. reverse() method 70. some() method 71. toSource() method 72. toString() method 73. pop() method 74. push() method 75. shift () method 76. slice () method 77. splice () method 78. sort() method 79. unshift() method 80. EVENT handling with JavaScript 81. onblur JavaScript Event 82. onchange JavaScript Event 83. onclick JavaScript Event 84. onDblClick JavaScript Event 85. onfocus JavaScript Event 86. onkeydown JavaScript Event 87. onkeyup JavaScript Event 88. Onkeypress JavaScript Event 89. onLoad JavaScript Event 90. onMousedown JavaScript Event 91. onMouseup JavaScript Event 92. Onmouseover JavaScript Event 93. onmouseout JavaScript Event 94. onMouseMove JavaScript Event 95. onReset JavaScript Event 96. onsubmit JavaScript Event 97. onUnload JavaScript Event 98. The Screen Object 99. JavaScript Interview Part1 100. JavaScript Interview Part2 101. JavaScript Interview Part3 102. JavaScript Interview Part4 103. JavaScript Interview Part5 104. JavaScript Interview Part6 105. JavaScript Interview Part7 106. JavaScript Interview Part8 107. JavaScript Interview Part9 108. JavaScript Interview Part10
Pr.Pg Next Pg

JavaScript Interview Questions and Answers Part 9

 

 

81. How to use strings as array indexes using JavaScript?

  • Javascript does not have a true hashtable object, but through its wierdness, you can use the array as a hashtable.

<script type="text/javascript">

var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];

 

for(var i=0; i < days.length; i++) {

days[days[i]] = days[i];

}

 

document.write("days[\"Monday\"]:"+days["Monday"]);

</script>

  • This produces

  • days["Monday"]:Monday

 

82. How to use "join()" to create a string from an array using JavaScript?

  • "join" concatenates the array elements with a specified seperator between them.

 

<script type="text/javascript">

var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];

document.write("days:"+days.join(","));

</script>

  • This produces

  • days:Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday

 

83. How to make a array as a stack using JavaScript?

  • The pop() and push() functions turn a harmless array into a stack

<script type="text/javascript">

var numbers = ["one", "two", "three", "four"];

numbers.push("five");

numbers.push("six");

document.write(numbers.pop());

document.write(numbers.pop());

document.write(numbers.pop());

</script>

  • This produces

  • sixfivefour

 

84. How to shift and unshift using JavaScript?

  • JavaScript array shift() method removes the first element from an array and returns that element.

  • JavaScript array unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.)

  • Note: The unshift() method does not work properly in Internet Explorer 8 and earlier

  • Example:

<script type="text/javascript">

var numbers = ["one", "two", "three", "four"];

numbers.unshift("zero");

document.write(" "+numbers.shift());

document.write(" "+numbers.shift());

document.write(" "+numbers.shift());

  • </script>

  • This produces

  • zero one two

  • shift, unshift, push, and pop may be used on the same array. Queues are easily implemented using combinations.

 

85. What does break and continue statements do?

Break statements:

  • The break statement tells JavaScript to exit a "controlled structure" and resume execution at a point after the structure.

  • The break statement is used with structures built using the following commands:

For

For …in

While

  • The break statement is most commonly used to prematurely end a for loop

 

Continue Statements:

  • This command can be used in “for” or “while” loops. It transfers the control to the start of the loop.

  • Sometimes you want to ignore rest of the statement and start executing it from top of the loop, then “continue” enables you to move control from one part of script to another.

  • Whenever script comes across “continue” statement, it transfers control to upward direction start of its corresponding loop.

 

86. How to create a function using function constructor?

  • The following example illustrates this, It creates a function called square with argument x and returns x multiplied by itself.

var square = new Function ("x","return x*x");

 

87. What are decodeURI() and encodeURI() functions in JavaScript?

  • Many characters cannot be sent in a URL, but must be converted to their hex encoding. These functions are used to convert an entire URI (a superset of URL) to and from a format that can be sent via a URI.

<script type="text/javascript">

var uri = "http://www.google.com/search?q=Tutorial"

document.write("Original uri: "+uri);

document.write("<br />encoded: "+encodeURI(uri));

</script>

 

88. What is toString() In JavaScript?

JavaScript array toString() method returns a string representing the source code of the specified array and its elements.

Syntax:

Array.toString();

The Array object overrides the toString method of Object. For Array objects, the toString method joins the array and returns one string containing each array element separated by commas.

Example:

For example, the following code creates an array and uses toString to convert the array to a string.

var monthNames = ['Jan', 'Feb', 'Mar', 'Apr'];

var myVar = monthNames.toString(); // assigns "Jan,Feb,Mar,Apr" to myVar.

 

89. What is sort() in JavaScript ?

Th sor method sorts the Array object in place; no ne Array object is created during execution.

Syntax:

array.sort([compareFunction])

  • If you supply a function in the compareFunction argument, it must return one of the following values:

  • A negative value if the first argument passed is less than the second argument.

  • Zero if the two arguments are equivalent.

  • A positive value if the first argument is greater than the second argument.


90. What is push() method in JavaScript?

  • The push() method is used to append one or more elements to the end of an array.

  • For example

var fruits = [ "apple" ];

fruits.push( "banana" );

fruits.push( "mango", "strawberry” );

console.log(fruits);

  • Output:

  • ["apple ", "banana ", "mango ", "strawberry "]

  • Using push() method we can also append multiple elements by passing multiple arguments.

  • The elements will be appended in the order of they are inserted i.e. left to right.


Pr.Pg border                                              Next Pg