asp.net.ph

Skip Navigation LinksHome > Abakada: Back to Basics > Basic Scripting > JavaScript Basics

JavaScript Basics

Abakada ~ Back to Basics


Expressions and Operators

This section describes JavaScript expressions and operators, including assignment, comparison, arithmetic, bitwise, logical, string, and special operators.

Expressions

An expression is any valid set of variables, operators, literals, and expressions that evaluates to a single value; the value can be a string, a number, or a logical value.

Expressions use symbol characters like = or +, called operators, to combine other expressions.

JavaScript accepts the following types of expressions:

  • String expressions, which evaluate to a character string
  • Arithmetic expressions, which evaluate to a number
  • Logical expressions, which evaluate to either true or false

Operators

Operators are used in expressions to indicate what action to perform on the expresion. JavaScript has both binary and unary operators.

A binary operator requires two operands, one before and one after the operator. For example, 3 + 4 or x * y.

A unary operator requires a single operand, either before or after the operator. For example, x++ or ++x.

In addition, JavaScript has one ternary operator, the conditional operator. A ternary operator requires three operands.

The following sections describe each of the types of operators used in JavaScript.


Operator Precedence

Operators in JavaScript are evaluated in a particular order of priority, known as operator precedence. An operator with higher precedence is evaluated before one with lower precedence. For example:

z = 12 * ( 34 + 56 + 789 )

There are five operators in this expression: =, *, ( ), +, and +. which will be evaluated in the following order: ( ), *, +, +, =.

  • First, the expression within the parentheses is evaluated, which are two addition operators with the same precedence.
  • Next, the value 123 is multiplied to the result of the expression within the parentheses.
  • Last, the resulting value is assigned to the variable z.

The following table describes the order of precedence from highest to lowest ( top to bottom in the table ). Operators with the same precedence ( in the same row ) are evaluated from left to right in an expression.

Operator type Individual operators
field access, array indexing, function call . [ ] ( )
object creation delete new
increment, decrement, negation, data type, undefined value ++ — ! ~ - + typeof void
multiplication, division, modulo division * / %
addition, subtraction, string concatenation + - +
bitwise shift << >> >>>
comparison, relational < <= > >= in instanceof
equality, inequality, identity, nonidentity == != === !==
bitwise and/xor/or & ^ |
logical and/or && ||
conditional ?:
assignment, assignment with operator = += -= *= /= %= <<= >>= >>>= &= ^= |=
comma ( multiple evaluation ) ,

We may use parentheses ( ) to alter or override the order of evaluation. Expressions within parentheses are evaluated before their values are used in the remainder of the statement.

More ...
Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note