Operators in javascript
There are different types of JavaScript operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Conditional Operators
- Type Operators
Operator | Definition |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
** | Exponentiation (ES2016) |
/ | Division |
% | Modulus (Division Remainder) |
++ | Increment |
-- | Decrement |
The assignment operator (=) assigns a value to a variable.
let input = 10; // assign value 10 into variable input
Assign values to variables and add them together:
let inputOne = 10; // assign the value 10 to inputOne
let inputTwo = 25; // assign the value 25 to inputTwo
let output = inputOne + inputTwo; // assign the value 35 to output (10 + 25)
The multiplication operator (*) multiplies numbers.
let inputOne = 10; // assign the value 10 to inputOne
let inputTwo = 25; // assign the value 25 to inputTwo
let output = inputOne * inputTwo; // assign the value 250 to output (10 * 25)
Adding JavaScript Strings
The + operator can also be used to add (concatenate) strings.
let inputOne = "Siva";
let inputTwo = "bharathy";
let output = inputOne + " " + inputTwo;
The result of output will be:
Siva bharathy
The += assignment operator can also be used to add (concatenate) strings:
let inputOne = "Hello siva ";
inputOne += "how are you";
The result of inputOne will be:
Hello siva how are you
Adding Strings and Numbers
Adding two numbers, will return the sum, but adding a number and a string will return a string:
const x = 5 + 5;
const y = "5" + 5;
const z = "siva" + 5;
The result of x, y, and z will be:
10
55
siva5
JavaScript Comparison Operators
Operator | Definition |
---|---|
== | 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 |