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

JavaScript Strings

JavaScript strings are used for storing and manipulating text. JavaScript Strings A JavaScript string is zero or more characters written inside quotes. Example var answer = “It’s alright”; var answer = “He is called ‘Johnny’”; var answer = ‘He is called “Johnny”‘; Try it Yourself » String Length The length of a string is found in the built in property length: Example […]

JavaScript Events

HTML events are “things” that happen to HTML elements. When JavaScript is used in HTML pages, JavaScript can “react” on these events. HTML Events An HTML event can be something the browser does, or something a user does. Here are some examples of HTML events: An HTML web page has finished loading An HTML input field was changed An […]

JavaScript Scope

Scope determines the accessibility (visibility) of variables. JavaScript Function Scope In JavaScript there are two types of scope: Local scope Global scope JavaScript has function scope: Each function creates a new scope. Scope determines the accessibility (visibility) of these variables. Variables defined inside a function are not accessible (visible) from outside the function. Local JavaScript […]

JavaScript Objects

Real Life Objects, Properties, and Methods In real life, a car is an object. A car has properties like weight and color, and methods like start and stop: Object Properties Methods car.name = Fiat car.model = 500 car.weight = 850kg car.color = white car.start() car.drive() car.brake() car.stop() All cars have the same properties, but the property values differ from car to car. All […]

JavaScript Functions

A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when “something” invokes it (calls it). Example function myFunction(p1, p2) { return p1 * p2;              // The function returns the product of p1 and p2 } Try it Yourself » JavaScript Function Syntax A JavaScript function is defined […]

JavaScript Data Types

JavaScript Data Types JavaScript variables can hold many data types: numbers, strings, objects and more: var length = 16;                               // Number var lastName = “Johnson”;                      // String var x = {firstName:”John”, lastName:”Doe”};    // Object The Concept of Data Types In programming, data types is an important concept. To be able to operate on variables, it is important to know something about the type. Without […]

JavaScript Assignment

JavaScript Assignment Operators Assignment operators assign values to JavaScript variables. Operator Example Same As = x = y x = y += x += y x = x + y -= x -= y x = x – y *= x *= y x = x * y /= x /= y x = x […]