Category Archives: Java Script

JavaScript Booleans

A JavaScript Boolean represents one of two values: true or false. Boolean Values Very often, in programming, you will need a data type that can only have one of two values, like YES / NO ON / OFF TRUE / FALSE For this, JavaScript has a Boolean data type. It can only take the values true or false. The Boolean() Function You can… Read More »

JavaScript Random

Math.random() Math.random() returns a random number between 0 (inclusive),  and 1 (exclusive): Example <!DOCTYPE html> <html> <body> <h2>JavaScript Math.random()</h2> <p>Math.random() returns a random number between 0 (included) and 1 (excluded):</p> <p id=”demo”></p> <script> document.getElementById(“demo”).innerHTML = Math.random(); </script> </body> </html> Result: JavaScript Math.random() Math.random() returns a random number between 0 (included) and 1 (excluded): 0.7717152983822737 Math.random() always returns… Read More »

JavaScript Math Object

The JavaScript Math object allows you to perform mathematical tasks on numbers. Example <!DOCTYPE html> <html> <body> <h2>JavaScript Math.PI</h2> <p>Math.PI returns the ratio of a circle’s circumference to its diameter:</p> <p id=”demo”></p> <script> document.getElementById(“demo”).innerHTML = Math.PI; </script> </body> </html> Result: JavaScript Math.PI Math.PI returns the ratio of a circle’s circumference to its diameter: 3.141592653589793 The… Read More »

JavaScript Set Date Methods

Set Date methods let you set date values (years, months, days, hours, minutes, seconds, milliseconds) for a Date Object. Set Date Methods Set Date methods are used for setting a part of a date: Method Description setDate() Set the day as a number (1-31) setFullYear() Set the year (optionally month and day) setHours() Set the… Read More »

JavaScript Get Date Methods

These methods can be used for getting information from a date object: Method Description getFullYear() Get the year as a four digit number (yyyy) getMonth() Get the month as a number (0-11) getDate() Get the day as a number (1-31) getHours() Get the hour (0-23) getMinutes() Get the minute (0-59) getSeconds() Get the second (0-59) getMilliseconds() Get the millisecond (0-999) getTime() Get the time (milliseconds since January 1, 1970)… Read More »

JavaScript Date Formats

JavaScript Date Input There are generally 3 types of JavaScript date input formats: Type Example ISO Date “2015-03-25” (The International Standard) Short Date “03/25/2015” Long Date “Mar 25 2015” or “25 Mar 2015” The ISO format follows a strict standard in JavaScript. The other formats are not so well defined and might be browser specific.… Read More »

JavaScript Date Objects

Example <!DOCTYPE html> <html> <body> <h2>JavaScript new Date()</h2> <p id=”demo”></p> <script> const d = new Date(); document.getElementById(“demo”).innerHTML = d; </script> </body> </html> Result: JavaScript new Date() Wed Aug 24 2022 09:50:25 GMT-0700 (Pacific Daylight Time) JavaScript Date Output By default, JavaScript will use the browser’s time zone and display a date as a full text… Read More »

JavaScript Array Const

ECMAScript 2015 (ES6) in 2015, JavaScript introduced an important new keyword: const. It has become a common practice to declare arrays using const: Example <!DOCTYPE html> <html> <body> <h2>JavaScript const</h2> <p id=”demo”></p> <script> const cars = [“Saab”, “Volvo”, “BMW”]; document.getElementById(“demo”).innerHTML = cars; </script> </body> </html> Result: JavaScript const Saab,Volvo,BMW Cannot be Reassigned An array declared with const cannot be… Read More »

JavaScript Array Iteration

Array iteration methods operate on every array item. JavaScript Array forEach() The forEach() method calls a function (a callback function) once for each array element. Example <!DOCTYPE html> <html> <body> <h2>JavaScript Array.forEach()</h2> <p>Calls a function once for each array element.</p> <p id=”demo”></p> <script> const numbers = [45, 4, 9, 16, 25]; let txt = “”; numbers.forEach(myFunction); document.getElementById(“demo”).innerHTML… Read More »

JavaScript Sorting Arrays

Sorting an Array The sort() method sorts an array alphabetically: Example <!DOCTYPE html> <html> <body> <h2>JavaScript Array Sort</h2> <p>The sort() method sorts an array alphabetically:</p> <p id=”demo1″></p> <p id=”demo2″></p> <script> const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; document.getElementById(“demo1”).innerHTML = fruits; fruits.sort(); document.getElementById(“demo2”).innerHTML = fruits; </script> </body> </html> Result: JavaScript Array Sort The sort() method sorts an array… Read More »