Some javascript tips, very useful in programming.
1. If you add a number and a string, the result will be a string.
Eg:- 5+5=10
Eg:- "5"+5=55
Eg:- 5+"5"=55
Eg:- "5"+"5"=55
2. Operators
x+=y is same as x=x+y
x-=y is same as x=x-y
x*=y is same as x=x*y
x/=y is same as x=x/y
3. Date Time Formats
var d = new Date();
var time = d.getFullYear()//The four digit year (1970-9999)
var time = d.getMonth()//Number of month (0-11)
var time = d.getDate()//Day of the month (0-31)
var time = d.getDay()//Day of the week(0-6). 0 = Sunday, ... , 6 = Saturday
var time = d.getHours()//Number of hours (0-23)
var time = d.getMinutes()//Number of minutes (0-59)
var time = d.getSeconds()//Number of seconds (0-59)
var time = d.getTime()//Number of milliseconds since 1/1/1970 @ 12:00 AM
alert(time);
4. break and continue
break inside a loop will break the current loop and continue executing the code that follows after the loop.
<script type="text/javascript">
var i=0;
for (i=0;i<=6;i++)
{
if (i==3)
{
break;//Stops execution of this loop and gets out of the loop.(i=0,i=1,i=2)
}
document.write("The number is " + i);
document.write("<br />");
}
</script>
The continue statement will break the current loop and continue with the next value.
<script type="text/javascript">
var i=0
for (i=0;i<=6;i++)
{
if (i==3)
{
continue;//Stops execution of this loop and continue with next values(i=0,i=1,i=2,i=4,i=5,i=6)
}
document.write("The number is " + i);
document.write("<br />");
}
</script>