JavaScript Type Conversion

Number() converts to a Number, String() converts to a String, Boolean() converts to a Boolean. JavaScript Data Types In JavaScript there are 5 different data types that can contain values: string number boolean object function There are 3 types of objects: Object Date Array And 2 data types that cannot contain values: null undefined The […]

JavaScript Break and Continue

The break statement “jumps out” of a loop. The continue statement “jumps over” one iteration in the loop. The Break Statement You have already seen the break statement used in an earlier chapter of this tutorial. It was used to “jump out” of a switch() statement. The break statement can also be used to jump […]

JavaScript While Loop

Loops can execute a block of code as long as a specified condition is true. The While Loop The while loop loops through a block of code as long as a specified condition is true. Syntax while (condition) {     code block to be executed } Example In the following example, the code in the […]

JavaScript For Loop

Loops can execute a block of code a number of times. JavaScript Loops Loops are handy, if you want to run the same code over and over again, each time with a different value. Often this is the case when working with arrays: Instead of writing: text += cars[0] + “<br>”; text += cars[1] + “<br>”; text […]

JavaScript Switch Statement

he switch statement is used to perform different actions based on different conditions. Switch Statement Use the switch statement to select one of many code blocks to be executed. Syntax switch(expression) { case n:         code block         break; case n:         code block         break; default: code block } This is how it works: The switch […]

JavaScript If…Else Statements

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 […]

JavaScript Comparison and Logical Operators

Comparison and Logical operators are used to test for true or false. 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 Try it == equal to x == 8 false Try it » x […]

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 […]

JavaScript Random

Math.random() Math.random() returns a random number between 0 (inclusive),  and 1 (exclusive): Example Math.random();              // returns a random number Try it Yourself » Math.random() always returns a number lower than 1. JavaScript Random Integers Math.random() used with Math.floor() can be used to return random integers. Example Math.floor(Math.random() * 10);     // returns a random integer from 0 to […]

JavaScript Math Object

The JavaScript Math object allows you to perform mathematical tasks on numbers. Example Math.PI;            // returns 3.141592653589793 Try it Yourself » Math.round() Math.round(x) returns the value of x rounded to its nearest integer: Example Math.round(4.7);    // returns 5 Math.round(4.4);    // returns 4 Try it Yourself » Math.pow() Math.pow(x, y) returns the value of x to the power of […]

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 […]

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) […]

JavaScript Date Formats

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. JavaScript Date Output […]

JavaScript Date Objects

JavaScript Date Output By default, JavaScript will use the browser’s time zone and display a date as a full text string: Thu Jul 05 2018 12:12:01 GMT+0500 (Pakistan Standard Time) You will learn much more about how to display dates, later in this tutorial. Creating Date Objects Date objects are created with the new Date() constructor. There […]

JavaScript Sorting Arrays

The sort() method sorts an array alphabetically: Example var fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.sort();        // Sorts the elements of fruits Try it Yourself » Reversing an Array The reverse() method reverses the elements in an array. You can use it to sort an array in descending order: Example var fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; fruits.sort();        // First sort the elements of fruits fruits.reverse();     // Then reverse the […]

JavaScript Array Methods

Converting Arrays to Strings The JavaScript method toString() converts an array to a string of (comma separated) array values. Example var fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; document.getElementById(“demo”).innerHTML = fruits.toString(); Result Banana,Orange,Apple,Mango Try it Yourself » The join() method also joins all array elements into a string. It behaves just like toString(), but in addition you can specify the separator: Example var fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; […]

JavaScript Arrays

JavaScript arrays are used to store multiple values in a single variable. Example var cars = [“Saab”, “Volvo”, “BMW”]; Try it Yourself » What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing […]

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 toString() returns […]

JavaScript Numbers

JavaScript has only one type of number. Numbers can be written with or without decimals. Example var x = 3.14;    // A number with decimals var y = 3;       // A number without decimals Try it yourself » Extra large or extra small numbers can be written with scientific (exponent) notation: Example var x = 123e5;    // 12300000 var y = 123e-5;   // 0.00123 JavaScript Numbers are […]

JavaScript String Methods

String methods help you to work with strings. String Methods and Properties Primitive values, like “John Doe”, cannot have properties or 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. String Length The length property returns […]