Skip to main content

Command Palette

Search for a command to run...

Enough JavaScript to get you Started : #6 Operators

Updated
β€’4 min read
Enough JavaScript to get you Started : #6 Operators

What is Operator?

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

Why we use operators?

πŸ‘‰ to check conditions
πŸ‘‰ to assign values to variables
πŸ‘‰ to compare between 2 or more values
πŸ‘‰ to perform basic operations

Operators in JavaScript

Artboard – 4.png

πŸ‘‰ Arithmetic : used for performing mathematical calculations like addition , subtraction , multiplication , division , modulo etc.

πŸ‘‰ Example 

var numOne = 1;
var numTwo = 5;
var sum = numOne + numTwo;
var sub = numOne - numTwo;

πŸ‘‰ Comparison : used to compare 2 or more values , returns boolean value after checking

πŸ‘‰ Example 

1 == 1 // true
2 > 1 // true
2 > 3 // false
3 <=3 // true

πŸ‘‰ Logical : The concept of logical operators is simple. They allow a program to make a decision based on multiple conditions.

πŸ‘‰ Example 

πŸ‘‰ '!' (logical AND) Operator
operator | value 1 | value 2 | result
&&       |   true  |  true   | true
&&       |   false |  true   | false
&&       |   true  |  false  | false
&&       |   false |  false  | false

πŸ‘‰ '!' (logical OR) Operator
operator | value 1 | value 2 | result
||       |   true  |  true   | true
||       |   false |  true   | true
||       |   true  |  false  | true
||       |   false |  false  | false

πŸ‘‰ '!' (logical not) Operator
!true = false
!false = true

πŸ‘‰ Ternary: The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

πŸ‘‰ Example

true ? console.log("hey"):console.log("hi");
// returns "hey"

false ? console.log("hey"):console.log("hi");
// returns "hi"

πŸ‘‰ Assignment : Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value

πŸ‘‰ Example

var numOne = 1;
numOne += 5; // short hand for numOne = numOne+5; value = 6

Let me know in comment section if you have any doubt or feedback. it's always worth to give time to thriving developer community :)

Keep Coding ❀

Hey , Let' ConnectπŸ‘‹

Twitter / Github

T

Adarsh pandya, Good one. You might want to create a series with these awesome articles. Series page link: https://hashnode.com/my-series

1
A

Tapas Adhikary Glad you liked it ! oh i didn't knew about series , i'll definitely going to publish these articles as series.

Thank you so much :)

1
T

Adarsh pandya You are welcome!

1

Enough JavaScript to get you Started

Part 7 of 21

πŸ‘‰ want to start with js? but don't know where to? πŸ‘‰ found new JS syntax confusing? πŸ‘‰ want to start your career as JS dev? πŸ‘‰ learn frontend frameworks like React? here's the easy guide made with ❀.

Up next

Enough JavaScript to get you Started : #7 Conditionals

Story Time πŸ‘‰ Enough of these boring theories , let's start to understand something very useful which is used on day to day basic and useful to programming in general. πŸ‘‰ Let's start with a fun story , let's say you were reading this block and your ...

More from this blog

Adarsh Pandya's Blog

23 posts

Sometimes I Write <Code/>, Sometimes <Code/> Writes Me :)