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 8

 

71. How to change style on an element?

  • Between CSS and javascript is a weird symmetry. CSS style rules are layed on top of the DOM. The CSS property names like "font-weight" are transliterated into "myElement.style.fontWeight". The class of an element can be swapped out. For example:

document.getElementById("myText").style.color = "green";

document.getElementById("myText").style.fontSize = "20";

-or-

document.getElementById("myText").className = "regular";

 

72. How to make elements invisible?

  • Change the "visibility" attribute of the style object associated with your element. Remember that a hidden element still takes up space, use "display" to make the space disappear as well.

 

if ( x == y) {

myElement.style.visibility = 'visible';

} else {

myElement.style.visibility = 'hidden';

}

 

73. How to set the cursor to wait?

  • In theory, we should cache the current state of the cursor and then put it back to its original state.

document.body.style.cursor = 'wait';

//do something interesting and time consuming

document.body.style.cursor = 'auto';

 

74. How to reload the current page ?

window.location.reload(true);

 

75. How to convert a string to a number using JavaScript?

  • You can use the parseInt() and parseFloat() methods.

  • Notice that extra letters following a valid number are ignored, which is kinda wierd but convenient at times

parseInt("100") ==> 100

parseFloat("98.6") ==> 98.6

parseFloat("98.6 is a common temperature.") ==> 98.6

parseInt("aa") ==> Nan //Not a Number

parseInt("aa",16) ==> 170 //you can supply a radix or base

 

76. How to convert numbers to strings using JavaScript?

  • You can prepend the number with an empty string

var mystring = ""+myinteger;

or

var mystring = myinteger.toString();

 

  • You can specify a base for the conversion,

var myinteger = 14;

var mystring = myinteger.toString(16);

mystring will be "e".

 

77. How to test for bad numbers using JavaScript?

the global method, "isNaN()" can tell if a number has gone bad.

var temperature = parseFloat(myTemperatureWidget.value);

if(!isNaN(temperature)) {

alert("Please enter a valid temperature.");

}

 

78. What's the Date object using JavaScript?

  • JavaScript comes with a built-in Date object that allows you to easily work with date and time data and related functions.

  • Both dates and times in JavaScript are derived from the Date object, which behaves much like any other object. To implement the current date or time in your script, you must first create a new instance of the object. You can then extract the desired data from that particular instance.

  • All dates are stored as the number of milliseconds since January 1, 1970, 00:00:00.

  • The Date object is a built-in object by which you can create instances to store encoded data related to the date and time of a certain moment.

  • The Date object is built in, just like the Array object. It acts as a template when creating instances of the object. The most basic assignment statement regarding the Date object is obviously the one that creates an instance according to the default arguments:

 

Syntax:

varInstance_date = new Date()

  • The above statement simply assigns an instance of the Date object to the data structure named Instance_date.

 

Example:

new Date(06,01,02) // produces "Fri Feb 02 1906 00:00:00 GMT-0600 (Central Standard Time)"

new Date(06,01,02).toLocaleString() // produces "Friday, February 02, 1906 00:00:00"

new Date(06,01,02) - new Date(06,01,01) // produces "86400000"

 

79. How tp create Arrays using JavaScript ?

  • The below example shows how to create arrays in JavaScript:

  • Example:

<html>

<head>

<title>cbtSAM Javascript Array Example</title>

<script type="text/javascript">

var days = new Array();

days[0] = "Sunday"

days[1] = "Monday"

days[2] = "Tuesday"

days[3] = "Wednesday"

days[4] = "Thursday"

days[5] = "Friday"

days[6] = "Saturday"

document.write("First day of week is: "+days[1])

</script>

</head>

<body>

</body>

</html>

  • Output:

  • First day of week is: Monday

 

80. How to delete an entry using JavaScript?

  • The "delete" operator removes an array element, but oddly does not change the size of the array.

  • Example:

 

<script type="text/javascript">

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

document.write("Number of days:"+days.length); delete days[4];

document.write("<br />Number of days:"+days.length);

</script>

  • This produces

  • Number of days:7

  • Number of days:7


Pr.Pg border                                              Next Pg