Skip to main content

Node.js #3- Operators

Operators
An operator performs some operation on single or multiple operands (data value) and produces a result. For example 1 + 2, where + sign is an operator and 1 is left operand and 2 is right operand. + operator adds two numeric values and produces a result which is 3 in this case.

 
Syntax:
<Left operand> operator <right operand>
 
<Left operand> operator

Example 1: Arithmetic Operators
Arithmetic operators are used to perform mathematical operations between numeric operands.
Source code:
var x = 10, y = 10
console.log("Addition Operator",x + y)
console.log("Subtraction Operator",x - y)
console.log("Multiplication Operator",x * y)
console.log("Division Operator",x / y)
console.log("Modulus Operator",x % y)
console.log("Increment Operator",x++)
console.log("Decrement Operator",y--)
 
Output:
Addition Operator 20
Subtraction Operator 0
Multiplication Operator 100
Division Operator 1
Modulus Operator 0
Increment Operator 10
Decrement Operator 10

Example 2: Comparison Operators
Comparison operators are used to compare two operands and return Boolean value true or false.
Brief:
==           Compares the equality of two operands without considering type.
===        Compares equality of two operands with type.
!=            Compares inequality of two operands.
>             Checks whether left side value is greater than right side value. If yes then returns true otherwise false.
<             Checks whether left operand is less than right operand. If yes then returns true otherwise false.
>=           Checks whether left operand is greater than or equal to right operand. If yes then returns true otherwise false.
<=           Checks whether left operand is less than or equal to right operand. If yes then returns true otherwise false.


Source code:
var x = 10, y = 10
console.log("equality Operator of two operands without considering type ::",x == y)
console.log("equality Operator of two operands with type ::",x === y)
console.log("inequality Operator ::",x != y)
console.log("greaterthan Operator ::",x > y)
console.log("lesserthan Operator ::",x < y)
console.log("greaterthan equal Operator ::",x >= y)
console.log("lesserthan equal Operator ::",x <= y)

Output:
equality Operator of two operands without considering type :: true
equality Operator of two operands with type :: true
inequality Operator :: false
greaterthan Operator :: false
lesserthan Operator :: false
greaterthan equal Operator :: true
lesserthan equal Operator :: true

Example 3: Logical Operators
Logical operators are used to combine two or more conditions.
Brief:
&&         is known as AND operator. It checks whether two operands are non-zero (0, false, undefined, null or "" are considered as zero), if yes then returns 1 otherwise 0.
||           is known as OR operator. It checks whether any one of the two operands is non-zero (0, false, undefined, null or "" is considered as zero).
!              is known as NOT operator. It reverses the boolean result of the operand (or condition)
Source code:
var x = 10, y = 10
console.log("AND  Operator::",(x != y) && (x < y))
console.log("OR  Operator::",(x != y) || (x < y))
console.log("NOT  Operator::",(x!=y))
 
Output:
AND  Operator:: false
OR  Operator:: false
NOT  Operator:: false

Example 4: Assignment Operators
Assignment operators are used to assign values to variables with less key strokes.
Brief:
=             Assigns right operand value to left operand.
+=           Sums up left and right operand values and assign the result to the left operand.
-=            Subtract right operand value from left operand value and assign the result to the left operand.
*=           Multiply left and right operand values and assign the result to the left operand.
/=           Divide left operand value by right operand value and assign the result to the left operand.
%=          Get the modulus of left operand divide by right operand and assign resulted modulus to the left operand.
Source code:
var x = 10, y = 10
console.log("= Operator::",x = y)
console.log("+= Operator::",x += 1)
console.log("-= Operator::",x -= 1)
console.log("*= Operator::",x *= 5)
console.log("/= Operator::",x /= 5)
console.log("%= Operator::",x %= 2)
 
Output:
= Operator:: 10
+= Operator:: 11
-= Operator:: 10
*= Operator:: 50
/= Operator:: 10
%= Operator:: 0

Example 5: Conditional Operators (or) ternary operator
Special operator called ternary operator (or) Conditional Operators :? That assigns a value to a variable based on some condition. This is like short form of if-else condition.
Syntax:
<condition> ? <value1> : <value2>;

Brief:
Conditional operator starts with conditional expression followed by ? operator. Second part ( after ? and before : operator) will be executed if condition turns out to be true. If condition becomes false then third part (after :) will be executed.
Source code:
var a = 10, b = 5;
var c = a > b? a : b; 
var d = a > b? b : a; 
console.log(c,d)
 
Output:
10 5
 
Thank you, Come Back to Learn More About Tech Stuff with Tech Bird
Have A Nice Day ! 
:)

Comments

Popular posts from this blog

BIG DATA ANALYTICS

BIG DATA ANALYTICS Have you ever hit upon how Amazon and Flip kart could possible verdict what we want; how the Google auto completes our search; how the YouTube looks into videos we want to watch? When we open YouTube, we will be at sixes and sevens, when we find ads related to what we have searched earlier in the past days. This is where we find ourselves in the era of big data analytics. More than 3 trillion bytes of information are being generated everyday through our smart phones, tablets, GPS devices, etc.  Have we thought about what can be done with all these information? This is where the data analytics comes into play. Big data analytics is just the study of future build up to store data in order to extract the behaviour patterns. The entire social networking website gathers our data which are related to our interest which is usually done by using our past search or any other social information. Data analytics will lead to a walkover in near future....

Amazon Q Developer Agents Can Now do more

Amazon Q Developer Agents Can Now do more than Helping You write Code Amazon Q Developer - Yes, yes that code generating assistant only. ⚡AWS just gave Amazon Q Developer a brain 🧠 boost. πŸ₯΄ Have you ever got bored of writing documents for the code you have written?🧐 πŸ₯Ί Asked senior dev for code review, which never happened because they are occupied with their own tasks? 🧐 I can hear you, saying 'Everytime' πŸ˜…. 🎟️ Now you can get some helping hand, from Amazon Q Developer Agents. πŸ€– This AI coding buddy can now write docs faster than you can say ' README.md ' πŸ” Review code like a caffeinated senior dev at 1 AM and throw out unit tests quicker than you can break the build. 🦸‍♂️ It's like having a super intern who never sleeps, doesn't drink all your coffee and won't steal your comfortable seating chair in office πŸ˜‰. 🐣Previously, ✏️(/dev) - can generate real time code suggestions based on your comments and existing code, bootstra...

A Conversation between Simba and Mufasa about AI

A Conversation between Simba 🐯 & Mufasa 🦁  about AI (Artificial Intelligence) Simba: Dad, have you heard about this thing called AI? Mufasa: Yes, my son. It’s a tool that can help us in many ways. Simba: But what if it takes over everything? What if one day, all the animals in the Pride Lands start asking AI to do their jobs? Mufasa: Simba, remember that while AI can assist us, it cannot replace the heart and spirit of the Circle of Life. Simba: So, you’re saying I shouldn’t worry? Mufasa: Exactly. Embrace AI as a friend, not a foe. It can help you hunt for ideas, but it can’t replace your instincts or your roar! Simba: So, I can still be the king, even with AI around? Mufasa: Of course! Just remember, the true strength of a king lies in his ability to adapt and grow, not just in what tools he uses. Simba: Thanks, Dad! I guess I’ll just have to learn to work with AI instead of worrying about it! Mufasa: That’s the spirit, my son. Now, let’s go find some lunch—AI can’t help ...