Author Archives: Help_adm

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 »

JavaScript Array Methods

Converting Arrays to Strings The JavaScript method toString() converts an array to a string of (comma separated) array values. Example <!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>toString()</h2> <p>The toString() method returns an array as a comma separated string:</p> <p id=”demo”></p> <script> const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; document.getElementById(“demo”).innerHTML = fruits.toString(); </script> </body> </html> Result: JavaScript… Read More »

JavaScript Arrays

An array is a special variable, which can hold more than one value: Why Use Arrays? If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:let car1 = “Saab”;let car2 = “Volvo”;let car3 = “BMW”; However, what if you want to loop through the cars and find… Read More »

JavaScript Number Methods

Number methods help you work with numbers. Number Methods and Properties Primitive values (like 3.14 or 2014), cannot have properties and methods (because they are not objects). But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties. The toString() Method The toString() method… Read More »

JavaScript Numbers

JavaScript has only one type of number. Numbers can be written with or without decimals. Example <!DOCTYPE html> <html> <body> <h2>JavaScript Numbers</h2> <p>Numbers can be written with or without decimals:</p> <p id=”demo”></p> <script> let x = 3.14; let y = 3; document.getElementById(“demo”).innerHTML = x + “<br>” + y; </script> </body> </html> Result: JavaScript Numbers Numbers… Read More »