Basics

JavaScript borrows most of its syntax from Java, but is also influenced by Awk, Perl and Python.

JavaScript is case-sensitive and uses the Unicode character set.

In JavaScript, instructions are called statements and are separated by a semicolon (;). Spaces, tabs and newline characters are called whitespace. The source text of JavaScript scripts gets scanned from left to right and is converted into a sequence of input elements which are tokens, control characters, line terminators, comments or whitespace. ECMAScript also defines certain keywords and literals and has rules for automatic insertion of semicolons (ASI) to end statements. However, it is recommended to always add semicolons to end your statements; it will avoid side effects. For more information, see the detailed reference about JavaScript's lexical grammar.

Comments

The syntax of comments is the same as in C++ and in many other languages:

Declarations

There are three kinds of declarations in JavaScript.

varDeclares a variable, optionally initializing it to a value.letDeclares a block scope local variable, optionally initializing it to a value.constDeclares a read-only named constant.

Variables

You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

You can use most of ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the Unicode escape sequences as characters in identifiers.

Some examples of legal names are Number_hits, temp99, and _name.

Declaring variables

You can declare a variable in three ways:

  • With the keyword var. For example, var x = 42. This syntax can be used to declare both local and global variables.

  • By simply assigning it a value. For example, x = 42. This always declares a global variable. It generates a strict JavaScript warning. You shouldn't use this variant.

  • With the keyword let. For example, let y = 13. This syntax can be used to declare a block scope local variable. See Variable scope below.

Evaluating variables

A variable declared using the var statement with no initial value specified has the valueundefined.

An attempt to access an undeclared variable or an attempt to access an identifier declared with let statement before initialization will result in a ReferenceError exception being thrown:


var a;

console.log("The value of a is " + a); // The value of a is undefined

console.log("The value of b is " + b); // Uncaught ReferenceError: b is not defined

console.log("The value of c is " + c); // The value of c is undefined

var c;

console.log("The value of x is " + x); // Uncaught ReferenceError: x is not defined

let x;

Variable scope

When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.

JavaScript before ECMAScript 2015 does not have block statement scope; rather, a variable declared within a block is local to the function (or global scope) that the block resides within. For example the following code will log 5, because the scope of x is the function (or global context) within which x is declared, not the block, which in this case is an if statement.

Global variables

Global variables are in fact properties of the global object. In web pages the global object iswindow, so you can set and access global variables using the window.``variable syntax.

Consequently, you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable calledphoneNumber is declared in a document, you can refer to this variable from an iframe asparent.phoneNumber.

Constants

You can create a read-only, named constant with the const keyword. The syntax of a constant identifier is the same as for a variable identifier: it must start with a letter, underscore or dollar sign and can contain alphabetic, numeric, or underscore characters.

A constant cannot change value through assignment or be re-declared while the script is running. It has to be initialized to a value.

The scope rules for constants are the same as those for let block scope variables. If the constkeyword is omitted, the identifier is assumed to represent a variable.

You cannot declare a constant with the same name as a function or variable in the same scope. For example:

// THIS WILL CAUSE AN ERROR

function f() {};

const f = 5;

// THIS WILL CAUSE AN ERROR ALSO

function f() {

const g = 5;

var g;

//statements

}

Data structures and types

Data types

The latest ECMAScript standard defines seven data types:

  • Six data types that are primitives:

    • Boolean. true and false.

    • null. A special keyword denoting a null value. Because JavaScript is case-sensitive,null is not the same as Null, NULL, or any other variant.

    • undefined. A top-level property whose value is undefined.

    • Number. 42 or 3.14159.

    • String. "Howdy"

    • Symbol (new in ECMAScript 2015). A data type whose instances are unique and immutable.

  • and Object

Data type conversion

JavaScript is a dynamically typed language. That means you don't have to specify the data type of a variable when you declare it, and data types are converted automatically as needed during script execution. So, for example, you could define a variable as follows:


var answer = 42;

answer = "Thanks for all the fish...";

Because JavaScript is dynamically typed, this assignment does not cause an error message.

In expressions involving numeric and string values with the + operator, JavaScript converts numeric values to strings. For example, consider the following statements:

Converting strings to numbers

In the case that a value representing a number is in memory as a string, there are methods for conversion.

  • parseInt()

  • parseFloat()

parseInt will only return whole numbers, so its use is diminished for decimals. Additionally, a best practice for parseInt is to always include the radix parameter. The radix parameter is used to specify which numerical system is to be used.

**Literals

You use literals to represent values in JavaScript. These are fixed values, not variables, that youliterally provide in your script. This section describes the following types of literals:

Array literals

An array literal is a list of zero or more expressions, each of which represents an array element, enclosed in square brackets ([]). When you create an array using an array literal, it is initialized with the specified values as its elements, and its length is set to the number of arguments specified.

Extra commas in array literals

You do not have to specify all elements in an array literal. If you put two commas in a row, the array is created with undefined for the unspecified elements. The following example creates the fish array:


var fish = ["Lion", , "Angel"];

This array has two elements with values and one empty element (fish[0] is "Lion", fish[1] isundefined, and fish[2] is "Angel").

Understanding the behavior of extra commas is important to understanding JavaScript as a language, however when writing your own code: explicitly declaring the missing elements asundefined will increase your code's clarity and maintainability.

Boolean literals

The Boolean type has two literal values: true and false.

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object. The Boolean object is a wrapper around the primitive Boolean data type. SeeBoolean for more information.

Integers

Integers can be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2).

  • Decimal integer literal consists of a sequence of digits without a leading 0 (zero).

  • Leading 0 (zero) on an integer literal, or leading 0o (or 0O) indicates it is in octal. Octal integers can include only the digits 0-7.

  • Leading 0x (or 0X) indicates hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F.

  • Leading 0b (or 0B) indicates binary. Binary integers can include digits only 0 and 1.

    
    0, 117 and -345 (decimal, base 10)
    
    015, 0001 and -0o77 (octal, base 8) 
    
    0x1123, 0x00111 and -0xF1A7 (hexadecimal, "hex" or base 16)
    
    0b11, 0b0011 and -0b11 (binary, base 2)
    

loating-point literals

A floating-point literal can have the following parts:

  • A decimal integer which can be signed (preceded by "+" or "-"),

  • A decimal point ("."),

  • A fraction (another decimal number),

  • An exponent.

Object literals

An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). You should not use an object literal at the beginning of a statement. This will lead to an error or not behave as you expect, because the { will be interpreted as the beginning of a block.

The following is an example of an object literal. The first element of the car object defines a property, myCar, and assigns to it a new string, "Saturn"; the second element, the getCarproperty, is immediately assigned the result of invoking the function (carTypes("Honda"));the third element, the special property, uses an existing variable (sales).


var sales = "Toyota";

function carTypes(name) {

  if (name === "Honda") {

    return name;

  } else {

    return "Sorry, we don't sell " + name + ".";

  }

}

var car = { myCar: "Saturn", getCar: carTypes("Honda"), special: sales };

console.log(car.myCar);   // Saturn

console.log(car.getCar);  // Honda

console.log(car.special); // Toyota

String literals

A string literal is zero or more characters enclosed in double (") or single (') quotation marks. A string must be delimited by quotation marks of the same type; that is, either both single quotation marks or both double quotation marks. The following are examples of string literals:


"foo"

'bar'

"one line \n another line"

"John's cat"

You can call any of the methods of the String object on a string literal value—JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the String.length property with a string literal:

Using special characters in strings

In addition to ordinary characters, you can also include special characters in strings, as shown in the following example.


"one line \n another line"

| CharacterMeaning | |

| --- | --- |

| \0 | Null Byte |

| \b | Backspace |

| \f | Form feed |

| \n | New line |

| \r | Carriage return |

| \t | Tab |

| \v | Vertical tab |

| \' | Apostrophe or single quote |

| \" | Double quote |

| \\ | Backslash character |

| \XXX | The character with the Latin-1 encoding specified by up to three octal digits _XXX_between 0 and 377. For example, \251 is the octal sequence for the copyright symbol. |

| \xXX | The character with the Latin-1 encoding specified by the two hexadecimal digitsXX between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol. |

| \uXXXX | The Unicode character specified by the four hexadecimal digits XXXX. For example, \u00A9 is the Unicode sequence for the copyright symbol. SeeUnicode escape sequences. |

| \u{XXXXX} | Unicode code point escapes. For example, \u{2F804} is the same as the simple Unicode escapes \uD87E\uDC04. |

results matching ""

    No results matching ""