JavaScript String Reference

JavaScript Strings A JavaScript string stores a series of characters like “John Doe”. A string can be any text inside double or single quotes: var carname = “Volvo XC60”; var carname = ‘Volvo XC60’; String indexes are zero-based: The first character is in position 0, the second in 1, and so on. For a tutorial about Strings, read our JavaScript […]

JavaScript Statements Reference

JavaScript Statements In HTML, JavaScript statements are “instructions” to be “executed” by the web browser. This statement tells the browser to write “Hello Dolly.” inside an HTML element with id=”demo”: Example document.getElementById(“demo”).innerHTML = “Hello Dolly.”; Try it Yourself » For a tutorial about Statements, read our JavaScript Statements Tutorial. JavaScript Statement Identifiers JavaScript statements often start with a statement […]

JavaScript Operators Reference

JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more. JavaScript Arithmetic Operators Arithmetic operators are used to perform arithmetic between variables and/or values. Given that y = 5, the table below explains the arithmetic operators: Operator Description Example Result in y Result in x Try it + Addition x = y […]

JavaScript Math Reference

Math Object The Math object allows you to perform mathematical tasks. Math is not a constructor. All properties/methods of Math can be called by using Math as an object, without creating it. Syntax var x = Math.PI;            // Returns PI var y = Math.sqrt(16);      // Returns the square root of 16 For a tutorial about the Math object, read […]

JavaScript Global Reference

The JavaScript global properties and functions can be used with all the built-in JavaScript objects. JavaScript Global Properties Property Description Infinity A numeric value that represents positive/negative infinity NaN The NaN property represents “Not-a-Number” value. This property indicates that a value is not a legal number. The NaN property is the same as the Number.Nan […]

JavaScript Boolean Reference

JavaScript Booleans JavaScript booleans can have one of two values: true or false. The Boolean() Function You can use the Boolean() function to find out if an expression is true: Example Boolean(10 > 9)       // returns true Try it Yourself » Or even easier: Example (10 > 9)              // also returns true 10 > 9                // also returns true Try it Yourself » For […]

JavaScript Array Reference

Array Object The Array object is used to store multiple values in a single variable: var cars = [“Saab”, “Volvo”, “BMW”]; Array indexes are zero-based: The first element in the array is 0, the second is 1, and so on. For a tutorial about Arrays, read our JavaScript Array Tutorial. Array Properties Property Description constructor Returns the function that […]

JavaScript Function Invocation

The code inside a JavaScript function will execute when “something” invokes it. Invoking a JavaScript Function The code inside a function is not executed when the function is defined. The code inside a function is executed when the function is invoked. It is common to use the term “call a function” instead of “invoke a function“. It […]

JavaScript Function Definitions

JavaScript functions are defined with the function keyword. You can use a function declaration or a function expression. Function Declarations Earlier in this tutorial, you learned that functions are declared with the following syntax: function functionName(parameters) { code to be executed } Declared functions are not executed immediately. They are “saved for later use”, and will be executed later, when they are invoked (called upon). […]

JavaScript Date Reference

Date Object The Date object is used to work with dates and times. Date objects are created with new Date(). There are four ways of instantiating a date: var d = new Date(); var d = new Date(milliseconds); var d = new Date(dateString); var d = new Date(year, month, day, hours, minutes, seconds, milliseconds); For a tutorial about date and times, read our JavaScript Date Tutorial. Date Object Properties Property Description constructor Returns the function […]

JavaScript Array Iteration Methods

Array iteration methods operate on every array item. Array.forEach() The forEach() method Calls a function once for each array element. Example var txt = “”; var numbers = [4, 9, 16, 25]; numbers.forEach(myFunction); function myFunction(value, index, array) { txt = txt + item + “<br>”; } Try it Yourself » Note that the function takes 3 arguments: The item value The item index […]

JavaScript Style Guide and Coding Conventions

Always use the same coding conventions for all your JavaScript projects. JavaScript Coding Conventions Coding conventions are style guidelines for programming. They typically cover: Naming and declaration rules for variables and functions. Rules for the use of white space, indentation, and comments. Programming practices and principles Coding conventions secure quality: Improves code readability Make code maintenance easier […]

JavaScript Use Strict

“use strict”; Defines that JavaScript code should be executed in “strict mode”. The “use strict” Directive The “use strict” directive is new in JavaScript 1.8.5 (ECMAScript version 5). It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. The purpose of “use strict” is to indicate that the code should be […]

JavaScript Hoisting

Hoisting is JavaScript’s default behavior of moving declarations to the top. JavaScript Declarations are Hoisted In JavaScript, a variable can be declared after it has been used. In other words; a variable can be used before it has been declared. Example 1 gives the same result as Example 2: Example 1 x = 5; // Assign 5 to x […]

JavaScript Bitwise Operations

JavaScript Bitwise Operators Operator Name Description & AND Sets each bit to 1 if both bits are 1 | OR Sets each bit to 1 if one of two bits is 1 ^ XOR Sets each bit to 1 if only one of two bits is 1 ~ NOT Inverts all the bits << Zero […]

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