JavaScript Operators: The Basics You Need to Know
What are the things we are covering in this blog
Topics to Cover
What operators are
Arithmetic operators (
+, -, *, /, %)Comparison operators (
==, ===, !=, >, <)Logical operators (
&&, ||, !)Assignment operators (
=, +=, -=)
What are the operators are there in Javascript
What is operator
In every programing language this is the most important thing and commonlly used thing. Like addition, substraction and etc. But you might think that this is not an important thing because this is very simple and easy to understand and that is true.
Now you are thinking that what are you telling like you are telling that it is hard and some times you are telling that is very easy to understand and easy to learn. And if this is your question then yes this topic is both hard and easy.
It fully depends that how you are trying to understand this topic if you are understanding this topic as a burden then you might face some problems in future but if you are learning for more knowledge then that is very very good for you.
Ok lot's of motivation type thing is there let's got to the topic.
Types of operator in js
There are so many types of operator are there but in this blog we are going to learn about the above topic what are mentioned in above.
So with that let's start the Arithmatic Operator
Arithmatic Operator
As name mentioned that in this we are going to operate some arithmatic operation like +, -, /, * ,% .
Let's take an example and understand all types of operator.
Pluse Operator(+)
It's work is simple it just add the elements. But it works some thing different in the string can you guss what it does in that case. if you are thinking that it joint that 2 string then that is
console.log(5 + 6)// the output should be 11 and that is correct
console.log("abc" + "def") // the output is "abcdef" this is also called concation
Minus Operator(-)
As we know that is just use for subsctraction. But it does not work in the string as the plus operator.
console.log(6 - 5) // as we can predic that the ans will be 1
Multiplication Operator(*)
. It is a binary operator used to calculate the product of its operands.
// syntax
let product = operand1 * operand2;
// Let's See an exampel
let a = 5;
let b = 2;
let result = a * b; // result is 10
Now the intresting part comes in to the picture thoes are the fevorate parts.
console.log(6 * '2'); // Output: 12 (the string '2' is converted to a number)
Now you might thinking that why it is converting that string into to number the result shuold be 6 times 2 means '222222'. JavaScript automatically converts operands to numeric values if necessary before performing the division.
console.log(10 * "GeeksforGeeks"); // Output: NaN
Why it is NaN and what does it mean by this ? and this question is common, Let's understand the concept behind that. So as we know that it try to conver the datatype of the input to perform the operation, but in this case this is not a number in side the string so that why when we multiplie a non numeric value with a numeric value then the output comes Not a Number(NaN).
Division Operator(/)
This is a standard arithmetic operator. The syntax is dividend / divisor . JavaScript uses floating-point numbers for all number types (except BigInt), so the division operator will return a floating-point result even if the division is even.
let result1 = 10 / 2; // result1 is 5
let result2 = 5 / 2; // result2 is 2.5
let result3 = "100" / 20; // result3 is 5 (string is coerced to a number)
Remainder Operator (%)
This operator (often called the modulo operator in other languages, but it is technically a remainder operator in JS) returns the remainder left over after the division.
let remainder = 10 % 3; // remainder is 1
Comparison operators (==, ===, !=, >, <)
Loose equality operator(==)
If you have some experience in codding then i think you know that what is the work of this operator, but if you don't have prior knowledge then this operator compare between 2 values and if they are equal then it returns true. But in javascript it try to convert the data to another type means it does not check the datatype.
Lets check some example about this.
console.log(80 == 80); // output: true
console.log("hey" == "hey"); // output: true
console.log("1" == 1); // output: true
console.log(0 == false); // output: true
as we have discused above that it try to convert the type and then compare that
Strict equality comparison operator(===)
It checks if two values are equal based on both their value and their data type, without performing any type conversion (type coercion). The most important feature is that if the operands are of different types, === immediately returns false. Unlike the loose equality operator (==), === does not attempt to convert one type to match the other before comparison.
console.log(5 === 5) // Output: true
console.log('5' === 5) // Output: false
console.log(NaN === NaN) // Output: false A special case: NaN is never strictly equal to anything, including itself.
console.log(true === true) // Output: true
I think this is all about triple equal to.
Inequality operator Operator(!=)
It is totally oposite of equality operator. If the data are not equal then it will return true other wise it will return false.
console.log(5 != 5) // Output: false
console.log('5' != 5) // Output: false
console.log(NaN === NaN) // Output: true
The same concept comes into the picture. It try to convert the data to another type means it does not check the datatype.
Greater then and Less then Operator(< and >)
These are used as normally as we are using in mathmatics but in this case it returns the true and false.
console.log(5 < 6) // Output: True
console.log(5 < 3) // Output: false
console.log(5 < 5) // Output: false
console.log(5 <= 5) // Output; true
console.log(5 > 6) // Output: false
console.log(5 > 3) // Output: true
console.log(5 > 5) // Output: false
console.log(5 >= 5) // Output; true
Logical operators (&&, ||, !)
And Operator(&&)
This is called and operator, it checks it all the value are true then the result will be true if any one of them is false then that will became false.
console.log(true && true) // Output: true
console.log(true && false) // Output: false
console.log(true && (true && false)) // Output: flase
Or Operator(||)
If any one of the condition is true then it will became true other wise false.
console.log(true || true) // Output: true
console.log(true || false) // Output: true
console.log(true || (true && false)) // Output: true
Not Operator(!)
This make the true condition to false and false to true. It will be very easy to understand in the example.
console.log(!true) // Output: false
console.log(!false) // Output: true
console.log(!(true && false)) // Output: true
Assignment operators (=, +=, -=)
These Operator are used to assign values to an variablel. This would be good that understand throght the examples.
let a = 5; // assigning the value to a variable
let b = 6;
let c = a+b; // assigning the addition of 'a' and 'b' to a variable 'c'
For others like += and -= these are also work like same but before assigning the value we are performing some operation this concept is same for like.
However, before assigning the value, they perform an operation on the existing value of the variable.
