Sunday 27 August 2017

JS - Date functions and Date -Formats

<script type="text/javascript">
//date funciton
getFullYear() - Retrun -full year ( all 4 digits)
getMonth() - return 0-11
getDate() - retrun 1-31
getDay() - return ( 0-6 , 0-is sunday)
getHours()- return 0-23
getMinuts() - return 0-59
getSeconds - return 0-59
getMilliseconds() - retrun 0-9999


function dateFormat(){

var d= new Date();
var yyyy = d.getFullYear();

var month = d.getMonth();
if(month <10){
month = "0"+month;
}

var day = d.getDate();
if(day < 10){
day = "0"+day;
}

console.log( day + '/' + month + '/' +yyyy )

}
dateFormat();
</script>

JS - Error Handling - Try throw catch finally

<script type="text/javascript">

var numerator = Number(prompt("enter numerator"));
 var denominator = Number(prompt("enter denominator")); 



 try { 
if( denominator == 0){ 
throw{
error: "divided by 0",
message: " customize error msg "
}
}
else {
document.write("REsult is "+ (numerator/denominator));
}

 }
 catch (e){
console.log(e) // [Object object]
console.log(e.error, e.message)  // custome Error Message 
 }
 finally
{
// if there is exception or not finally block will execute
console.log("finally block");
}


</script>

JS - Array - push ===unshift or pop === shift

push the value to end of an array element, unshift push the element at beging of array
pop remove the last element of an array, shift remove the first element of an array. 

<script type="text/javascript">

var myArr = [2,3];
document.write("Orginal Arr "+myArr+"<br>")
myArr.push(4);
myArr.unshift(1)
document.write("unshift 1 and push 4 "+myArr+"<br>")

myArr.pop();
myArr.shift();
document.write("shift and pop "+ myArr+"<br>")

document.write(myArr.length)
</script>