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 Reserved Words

In JavaScript you cannot use these reserved words as variables, labels, or function names: abstract arguments await* boolean break byte case catch char class* const continue debugger default delete do double else enum* eval export* extends* false final finally float for function goto if implements import* in instanceof int interface let* long native new null […]

JavaScript Performance

How to speed up your JavaScript code. Reduce Activity in Loops Loops are often used in programming. Each statement in a loop, including the for statement, is executed for each iteration of the loop. Statements or assignments that can be placed outside the loop will make the loop run faster. Bad: var i; for (i = 0; i […]

JavaScript Common Mistakes

This chapter points out some common JavaScript mistakes. Accidentally Using the Assignment Operator JavaScript programs may generate unexpected results if a programmer accidentally uses an assignment operator (=), instead of a comparison operator (==) in an if statement. This if statement returns false (as expected) because x is not equal to 10: var x = 0; if (x == 10) Try it Yourself […]

JavaScript Best Practices

Avoid global variables,  avoid new,  avoid  ==,  avoid eval() Avoid Global Variables Minimize the use of global variables. This includes all data types, objects, and functions. Global variables and functions can be overwritten by other scripts. Use local variables instead, and learn how to use closures. Always Declare Local Variables All variables used in a function […]

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

The JavaScript this Keyword

Example var person = { firstName: “John”, lastName : “Doe”, id       : 5566, fullName : function() { return this.firstName + ” ” + this.lastName; } }; Try it Yourself » What is “this”? In a function definition, this refers to the “owner” of the function. In the example above, this refers to the person object. The person object “owns” the fullName method. Default Binding When used alone, this refers to the Global object. In a browser the Global object is […]

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 Debugging

Errors can (will) happen, every time you write some new computer code. Code Debugging Programming code might contain syntax errors, or logical errors. Many of these errors are difficult to diagnose. Often, when programming code contains errors, nothing will happen. There are no error messages, and you will get no indications where to search for […]

JavaScript Errors – Throw and Try to Catch

The try statement lets you test a block of code for errors. The catch statement lets you handle the error. The throw statement lets you create custom errors. The finally statement lets you execute code, after try and catch, regardless of the result. Errors Will Happen! When executing JavaScript code, different errors can occur. Errors can be coding errors made by the programmer, […]

JavaScript Regular Expressions

A regular expression is a sequence of characters that forms a search pattern. The search pattern can be used for text search and text replace operations. What Is a Regular Expression? A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this […]

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

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