Representing data with values
Primitive Data Types
- Primitive Data Types
- Number Type
- String Type
- Boolean data type
- Undefined and null
- What are statements?
- Expression Statements
- Compound and Empty Statements
- Declaration Statements
- Conditional statements
- Loop statements
- Jump Statements
23+45 //produces a value 68 parseInt(12.45)//produces a value 12
The expression x * y evaluates to the product of the values of the expressions x and y. For simplicity, we sometimes say that an operator “returns” a value rather than “evaluates to” a value.
1.23 // A number literal "hello" // A string literal /pattern/ // A regular expression literal
An arithmetic expression is an expression that results in a numeric value.
23+34 // results 57 3*9 // results 27 -(-23) // results 23
Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value. The logical operators are described in the following table.
Examples of expressions that can be converted to false are those that evaluate to null, 0, NaN, the empty string (“”), or undefined.
The following code shows examples of the && (logical AND) operator.
var a1 = true && true; // t && t returns true var a2 = true && false; // t && f returns false var a3 = false && true; // f && t returns false var a4 = false && (3 == 4); // f && f returns false var a5 = 'Cat' && 'Dog'; // t && t returns Dog var a6 = false && 'Cat'; // f && t returns false var a7 = 'Cat' && false; // t && f returns false
The following code shows examples of the || (logical OR) operator.
var o1 = true || true; // t || t returns true var o2 = false || true; // f || t returns true var o3 = true || false; // t || f returns true var o4 = false || (3 == 4); // f || f returns false var o5 = 'Cat' || 'Dog'; // t || t returns Cat var o6 = false || 'Cat'; // f || t returns Cat var o7 = 'Cat' || false; // t || f returns Cat
The following code shows examples of the ! (logical NOT) operator.
var n1 = !true; // !t returns false var n2 = !false; // !f returns true var n3 = !'Cat'; // !t returns false
As logical expressions are evaluated left to right, they are tested for possible “short-circuit” evaluation using the following rules:
- false && anything is short-circuit evaluated to false.
- true || anything is short-circuit evaluated to true.
The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect.
Note that for the second case, in modern code you can use the new Nullish coalescing operator (??) that works like ||, but it only returns the second expression, when the first one is “nullish”, i.e. null or undefined. It is thus the better alternative to provide defaults, when values like ” or 0 are valid values for the first expression, too.
NOTE: Some expressions, however, have side effects, and their evaluation may affect the result of future evaluations. The assignment operators are the most obvious example: if you assign a value to a variable or property, that changes the value of any expression that uses that variable or property.