Monthly Archives: August 2022

JavaScript Switch Statement

The switch statement is used to perform different actions based on different conditions. The JavaScript Switch Statement Use the switch statement to select one of many code blocks to be executed. Syntax switch(expression) {  case x:    // code block    break;  case y:    // code block    break;  default:    // code block} This is how it works: The switch expression is evaluated once. The value of the expression is compared with… Read More »

JavaScript if, else, and else if

Conditional statements are used to perform different actions based on different conditions. Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In JavaScript we have the following conditional statements: Use if to specify a block of code to… Read More »

JavaScript Comparison and Logical Operators

Comparison Operators Comparison operators are used in logical statements to determine equality or difference between variables or values. Given that x = 5, the table below explains the comparison operators: Operator Description Comparing Returns == equal to x == 8x == 5x == “5” falsetruetrue === equal value and equal type x ===5x === “5” truefalse… Read More »

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 »