JavaScript GlossaryS2C Home « JavaScript Glossary
Glossary Term | Description |
---|---|
Data Types | Recognized JavaScript values and their ranges. |
Expressions | Any valid unit of code that resolves to a value. |
Literals | Literal notation for various entities. |
Operator Precedence | Operator symbols order of preference. |
Data TypesTop
The following and suprisingly small list of data types and their ranges are recognized in JavaScript.
Data Type | Range or Example | Notes |
---|---|---|
Boolean | true or false | Boolean constructor which is a wrapper for a boolean value or boolean primitive. |
null | null | Special keyword also a primitive value denoting a null value. JavaScript is case-sensitive, so other variants such as Null and NULL are not recognized. |
Number | MAX_VALUE 1.7976931348623157e+308 -MAX_VALUE -1.7976931348623157e+308 MIN_VALUE 5e-324 -MIN_VALUE -5e-324 | Maximum positive number value that can be represented in JavaScript. Maximum negative number value that can be represented in JavaScript. Minimum positive number value that can be represented in JavaScript. Minimum negative number value that can be represented in JavaScript. |
String | 'string text', "string text" | String constructor for strings and character sequences. |
undefined | undefined | Global variable and primitive value. |
Data Type Conversion
JavaScript is not a strongly typed language such as Java, but is dynamically typed so after variable creation you can store data of any type within the variable by using the equals symbol (=) via assignment. You can also set a variables initial value at creation as well. An example of dynamic typing is shown below:
// Firstly we assign a numeric to aVar
var aVar = 1;
// Here we move a string to the variable which is fine as javaScript is dynamically typed
aVar = 'move in a string';
Where expressions involve numeric and string values and use the +
operator, JavaScript will convert numeric values to strings and treat the expression as a string concatenation. The JavaScript interpreter
uses automatic type conversion when combining string and numbers, which means all numbers are converted to strings before concatenation takes place.
// Create a string and numeric variables.
var aString = '5';
var aNumber = 5;
// Concatenation ends up with a string holding the value '55' not 10.
alert(aString + aNumber);
ExpressionsTop
An expression is any valid unit of code that resolves to a value whether the result is assigned to a variable or not as in the following examples:
// Expression assignment of 3
var aVar = 3;
// Expression value of 3
1 + 2;
We can split expressions into four categories which are listed below with typical usage:
Category | Description | Typical Usage |
---|---|---|
Arithmetic | Evaluates to a number. | Arithmetic Operators(+, -, *, /, %, ++, --, unary +, unary -) |
Logical | Evaluates to true or false . | Logical Operators(&&, ||, !) |
Object | Evaluates to an object. | Special Operators |
String | Evaluates to a character string. | String Operators(+ and +=) |
LiteralsTop
Literals are strings of characters that we use to represent actual value in JavaScript. For certain objects we can use literal notation instead of the new special operator to create instances of an object. The following subsections give examples of literal usage and where appropriate normal and literal instantiation for the objects in question.
Literal Type | Description |
---|---|
Array Literal | Array literal syntax can be used to create and initialize arrays. |
Boolean Literal | Used to set true and false values in booleans. |
Floating-point Literal | Non-integer numbers that have a fractional component. |
Integer | Octal (base 8), decimal (base 10) and hexadecimal (base 16). |
Object Literal | Object literal syntax can be used to create and initialize objects. |
String Literal | String literal syntax can be used to create and initialize strings. |
Array Literal
Array literal syntax can be used to create arrays by using square brackets [ ], which enclose a list of zero or more expressions where each expression represents an element within the array. The array is created from the enclosed expressions and initialized with the specified values as its elements. The length is derived from the number of expressions specified along with any delimiting commas.
// Create an array with three elements.
var anArray = new Array('one', 'two', 'three');
alert(anArray + ' - ' + anArray.length);
// Create an empty array using literal syntax.
var anEmptyArray = []
// Create an array with three elements using literal syntax.
var weekendDays = ['Fri', 'Sat', 'Sun'];
// Create an array with three elements using literal syntax and delimiter.
// The second element will have a value of undefined.
var weekendDays = ['Fri', , 'Sun'];
Boolean Literal
Boolean literal values are true
and false
and can be used to set values within a Boolean object or boolean primitive.
The code below illustrates the use of boolean literal values as well as highlighting the difference between the Boolean
constructor and the boolean primitives true
and false
.
// Create a Boolean object.
aBoolean = new Boolean(false);
if (aBoolean) {
alert('set false, but evaluates true'); // This code is executed
} else {
alert('set false'); // This code is NOT executed
}
// Set a boolean primitive.
bBoolean = false;
if (bBoolean) {
alert('set false, but evaluates true'); // This code is NOT executed
} else {
alert('set false'); // This code is executed
}
Floating-point Literal
Floating-point literals represent non-integer numbers that have a fractional component. In JavaScript A floating-point literal can consist of up to four parts comprised from the following syntax:
[(+|-)][digits][.digits][(E|e)[(+|-)]digits]
Component | Description | Examples |
---|---|---|
Decimal integer | Can be prefixed signed with a + or - . | 1, -99, +123 |
Decimal point | Used to separate the integer and fractional decimal. | . |
Fractional decimal integer | Used to set fractional decimal. | 1, 10, 100 |
Exponent | E or e followed by an integer which can be prefixed signed with a + or - . | e1, -E4, +e12 |
The above components can be mixed and matched to form a floating-point literal, but the floating-point literal must have at least one digit and either a decimal point or e
or E
.
Following are a few examples of well formed floating-point literals:
-1.23, .986, -5e-324, 1.7976931348623157e+308, .2E34 and 1E-234.
Integer
We can express Integers in octal (base 8), decimal (base 10) and hexadecimal (base 16).
Base | Description | Examples |
---|---|---|
Octal | Sequence of digits with a leading 0 indicates it is in octal.Octal integers can only include the digits 0-7 .Octal integer literals have been deprecated and removed although JavaScript still supports them for backward compatibility. | -01, 012, +0123 |
Decimal | Sequence of digits without a leading 0 . | -1, 12, +123 |
Hexadecimal | Sequence of digits with a leading 0x or 0X indicates it is in hexadecimal.Hexadecimal integers can include digits 0-9 and
the letters a-f and A-F . | -0xC1D3, 0X12, 0xfff |
Object Literal
Object literal syntax can be used to create and initialize objects. This style of coding using matching braces { } to mark object boundaries, with property names followed by a colon and values
delimited by commas, is known as JavaScript Object Notation. This is more commonly known as JSON
which is popular with page authors
because of its advantages over object assignment. Following are examples of object instantiation using assignment and object literal syntax:
/*
* Create an Address object via assignment.
*/
var anAddress = new Object();
anAddress.number = 12;
anAddress.street = 'Picnic Street';
anAddress.town = 'Toytown';
anAddress.owner = homeOwner;
homeOwner.firstName = 'Teddy';
homeOwner.lastName = 'Bear';
/*
* Create an Address object using object literal notation.
*
* Visually more informative of object structure and less code than the
* code from above. For more complex objects the visual and compactual
* differences are accentuated :).
*/
var anAddress = {
number: 12,
street: 'Picnic Street',
town: 'Toytown',
homeOwner: {
firstName: 'Teddy',
lastName: 'Bear'
}
};
String Literal
String literals are string primitives which can be used to create and initialize strings and are composed of zero or more characters enclosed in single or double quotation marks. The enclosing quotation marks
must be of the same type and cannot be mixed. So strings must be delimited by single quotation marks ''
or double quotation marks ""
.
Any string primitives have full access to String
methods as JavaScript automaticially converts any string primitives to temporary
String
objects when a String
method is called on the string primitive.
// Create a String object
aString = new String('happychappy');
// Create a string literal
bString = 'happychappy';
Operator PrecedenceTop
The table below shows the order of precedence that is invoked when JavaScript interprets operator symbols. The list includes some Special Operators which are discussed in greater detail in the relevant parts of the reference section.
When using multiple operators in an expression it is always best practice to use parentheses to explicitly state the order of precedence for clarity and readability. The table lists order of precedence from highest to lowest.
Precedence | Operators | Operation | Associativity |
---|---|---|---|
1 | . | member | left |
[ ] | |||
new | object creation | right | |
2 | () | function call | left |
3 | ++ | prefix/postfix increment | n/a |
-- | prefix/postfix decrement | ||
4 | + | unary plus | right |
- | unary minus | ||
! | boolean logical NOT | ||
~ | bitwise NOT | ||
delete | delete | ||
typeof | typeof | ||
void | void | ||
5 | / | division | left |
* | mutiplication | ||
% | modulus | ||
6 | + | addition or string concatenation | left |
- | subtraction | ||
7 | << | bitwise left shift | left |
>> | bitwise right shift | ||
>>> | bitwise signed right shift | ||
8 | / | less than | left |
<= | less than or equal to | ||
> | greater than | ||
>= | greater than or equal to | ||
in | property test | ||
instanceof | prototype test | ||
9 | == | equal to | left |
!= | not equal to | ||
=== | strict equal to | ||
!== | strict not equal to | ||
10 | & | boolean logical AND or bitwise AND | left |
11 | ^ | boolean logical XOR or bitwise XOR | left |
12 | | | boolean logical OR or bitwise OR | left |
13 | && | short-circuit boolean logical AND | left |
14 | || | short-circuit boolean logical OR | left |
15 | ? : | conditional | right |
16 | yield | yield | right |
17 | = | assignment | right |
+= | shorthand addition | ||
-= | shorthand subtraction | ||
/= | shorthand division | ||
*= | shorthand mutiplication | ||
%= | shorthand modulus | ||
&= | shorthand boolean logical AND or bitwise AND | ||
|= | shorthand boolean logical OR or bitwise OR | ||
^= | shorthand boolean logical XOR or bitwise XOR | ||
<<= | shorthand bitwise left shift | ||
>>= | shorthand bitwise right shift | ||
>>>= | shorthand bitwise signed right shift | ||
18 | , | comma | left |