JS Booleans and Dates
JavaScript provides several standard objects, such as, Array, Boolean, Date, Math, String, etc.
We shall review Boolean and Date here.
JS Boolean
A JS Boolean represents one of the two values: true or false.
We can use the Boolean()
function to find out if a variable (or an expression) is true or false.
Example: Illustration of Boolean Object
<html>
<body>
<script>
var a = null; //null
var b; //undefined
var c = 100/'A'; //NaN
//Test using Boolean() Function
document.write("==Test using Boolean() Function==" + "<br>");
var b1 = Boolean(99);
var b2 = Boolean(3.142);
var b3 = Boolean(0);
var b4 = Boolean("");
var b5 = Boolean("Bye");
var b6 = Boolean('false');
var b7 = Boolean(3 + 9 + 3.14);
var b8 = Boolean(a);
var b9 = Boolean(b);
var b10 = Boolean(c);
document.write("99 is: " + b1 + "<br>");
document.write("3.142 is: " + b2 + "<br>");
document.write("0 is: " + b3 + "<br>");
document.write("An empty string is: " + b4 + "<br>");
document.write("Any non-empty string is: " + b5 + "<br>");
document.write("The string 'false' is: " + b6 + "<br>");
document.write("Any expression (excluding zero) is: " + b7 + "<br>");
document.write("null is: " + b8 + "<br>");
document.write("undefined is: " + b9 + "<br>");
document.write("NaN is: " + b10 + "<br><br>");
//Test whether Booleans are objects
document.write("==Test whether Booleans are objects==" + "<br>");
var r = false;
var s = new Boolean(false);
document.write("Type of r is: " + typeof r + "<br>");
document.write("Type of s is: " + typeof s + "<br>");
</script>
</body>
</html>
Output:
==Test using Boolean() Function==
99 is: true
3.142 is: true
0 is: false
An empty string is: false
Any non-empty string is: true
The string 'false' is: true
Any expression (excluding zero) is: true
null is: false
undefined is: false
NaN is: false
==Test whether Booleans are objects==
Type of r is: boolean
Type of s is: object
JS Date
The following JS code shows the example of the Date object.
Example: Illustration of Date Object
<html>
<body>
<script>
var date = new Date(); //an instance of Date object
var week_day = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var month_name = new Array ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
document.write("Today's Date is = ");
document.write(date.getDate());
document.write("/");
document.write(date.getMonth() + 1);
document.write("/");
document.write(date.getFullYear());
document.write("<br>");
document.write(week_day[date.getDay()] + " ");
document.write(month_name[date.getMonth()] + " ");
document.write(date.getFullYear());
</script>
</body>
</html>
Output:
Today's Date is = 10/11/2019
Sunday Nov 2019
In the above code, we create a new instance of the Date object. From this object, we get the day number, the month number (and increment by 1, since it starts with 0), and the 4-digit year; all concatenated with each other by using a front slash (“/”) symbol. Here, we can use the Array default object as well.
The table below shows the most useful Date functions.
Method | Description |
Date() | Returns a Date object |
getDate() | Returns the date of a Date object (from 1-31) |
getDay() | Returns the day of a Date object (from 0-6, where 0=Sunday, 1=Monday etc.) |
getMonth() | Returns the month of a Date object (from 0-6, where 0=January, 1=February etc.) |
getFullYear() | Returns the year of a Date object (in 4 digits) |
getHours() | Returns the hour of a Date object (from 0-23) |
getMinutes() | Returns the minute of a Date object (from 0-59) |
getSeconds() | Returns the second of a Date object (from 0-59) |