Operators
In programming, symbols like plus (+), minus (-), multiply (×), and divide (÷) are called operators.
Operators are used to process data (values) and generate new values.
Basic Operators
- Arithmetic Operators
These perform basic mathematical operations like in arithmetic.
-
+: addition -
-: subtraction -
*: multiplication -
/: division -
%: remainder
let result = 5 * 3; // 5 x 3 = 15 is stored in result
- Comparison Operators
These compare the relationship between values or variables and return a true or false value.
-
==: equal (compares only values) -
===: strict equal (compares values and types) -
!=: not equal -
!==: strict not equal -
>: greater than -
<: less than -
>=: greater than or equal to -
<=: less than or equal to
let isTrue = 5 > 3; // true is stored in isTrue
- Logical Operators
These combine multiple conditions to determine a true or false value.
-
&&: AND - true only if both conditions are true -
||: OR - true if at least one condition is true -
!: NOT - inverts the truth value
let value = true && false; // false is stored in value
- Ternary Operator
A shorthand for conditional statements, it can be written as:
(condition) ? value1 : value2
let age = 15;
let type = age >= 20 ? 'adult' : 'teen'; // "teen" is stored in type
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.