Set - 7

Question 1 :

What's the Date object using JavaScript? 

Answer :

Time inside a date object is stored as milliseconds since Jan 1, 1970.

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"


Question 2 :

What does the delete operator do?

Answer :

The delete operator is used to delete all the variables and objects used in the program ,but it does not delete variables declared with var keyword.


Question 3 :

How to create Arrays using JavaScript ?

Answer :

<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 is "+days[0])
</script>

This produces
first day is Sunday
A more compact way of creating an array is the literal notation: 

<script type="text/javascript">
var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];
document.write("first day is "+days[0]) 
</script>

This produces
first day is Sunday


Question 4 :

How to delete an entry using JavaScript? 

Answer :

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

<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


Question 5 :

How to use strings as array indexes using JavaScript?

Answer :

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