Expression, Operator, and Function
An expression evaluates into a single value:
- The expression that assigns a value to a variable
- The expression that uses 2 or more values to return a single value
The operator creates a single value from one or more values. Types of operators:
- Arithmetic operators:
- +: addition
- -: subtraction
- *: multiplication
- **: exponentiation
- /: division
- %: modulus
- ++: increment
- --: decrement
- Assignment Operators:
- =
- +=
- -=
- *=
- /=
- %=
- **=
- String operators:
- +:
- Adding two numbers will return the sum
- Adding a number and a string will return a string
- +:
- Comparison operators:
- ==: equal to
- ===: equal value and equal type
- !=: not equal
- !==: not equal value or not equal type
- >: greater than
- <: less than
- >=: greater than or equal to
- <=: less than or equal to
- ?: ternary operator
- Logical operators:
- &&: logical and
-
: logical or - !: logical not
- Type operators:
- typeof: returns the type of a variable
- instanceof: returns true if an object is an instance of an object type
Functions let you group a series of statements together to perform a specific task. Tips of using a function:
- Declare a function using the function keyword
- Give the function a name followed by parentheses
- The statements that perform the task sit in a code block
- Functions can be used as variable values
- Variables declared within a function become LOCAL to the function