By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Expressions and operators
JavaScript

Expressions and operators

Rohan750
Last updated: 2021/05/16 at 5:34 AM
Rohan750
Share
6 Min Read

Contents
1.Assignment operators2.Arithmetic operatorsIncrement and Decrement operator3.Comparison operators4.Logical operators5.String Concatenation(operators)6.Conditional (ternary) operator
1)Assignment operators

2)Arithmetic operators
3)Comparison operators
4)Logical operators
5)String Concatenation(operators)
6)Conditional (ternary) operator
Expressions and operators

1.Assignment operators

An assignment operator assigns a value to its left operand based on the value of its right operand.
The simple assignment operator is equal (=).
Example:

var x = 5; 
var y = 5;

2.Arithmetic operators

An arithmetic operator takes numerical values (either literals or variables) as their operands and returns a single numerical value.

Example:


console.log(2+2);
console.log(10-4);
console.log(20/5);
console.log(5*5);

console.log("Remainder Operator " + 27%4);

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
4
6
4
25
Remainder Operator 3
PS C:UsersThe-BeastDesktopjavascript Blog> 

Increment and Decrement operator

Operator: x++ or ++x or x– or –x
If used postfix, with operator after operand (for example, x++), 
the increment operator increments and returns the value before incrementing.

Example:


var num = 15;
var newNum = num++;
console.log(num);
console.log(newNum);

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
16
15

PS C:UsersThe-BeastDesktopjavascript Blog>

Postfix increment operator means the expression is evaluated first using the original value of the variable and then the variable is incremented(increased).

If used prefix, with the operator before operand (for example, ++x), 

the increment operator increments and returns the value after incrementing.

Example:


var num = 15;
var newNum = ++num; 
console.log(num);
console.log(newNum);

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
16
16

PS C:UsersThe-BeastDesktopjavascript Blog>

Prefix increment operator means the variable is incremented first and then the expression is evaluated using the new value of the variable.

The decrement operator works the same as the increment operator.

3.Comparison operators

A comparison operator compares its operands and returns a logical value based on whether the comparison is true.

Example:

var a = 30;
var b = 10;

// Equal (==)
console.log(a == b);

// Not equal (!=)
console.log(a != b);

// Greater than (>)
console.log(a > b);

// Greater than or equal (>=)
console.log(a >= b);

// Less than (<)
console.log(a < b);

// Less than or equal (<=)
console.log(a <= b);

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
false
true 
true 
true 
false
false
PS C:UsersThe-BeastDesktopjavascript Blog> 

4.Logical operators

Logical operators have typically used with Boolean (logical) values. they return a Boolean value.

Logical AND (&&)

The logical AND (&&) operator (logical conjunction) for a set of operands is true if and only if all of its operands are true.

Example:


var a = 30;
var b = -20;
console.log(a > b && b > -50 && b < 0);

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
true
PS C:UsersThe-BeastDesktopjavascript Blog> 

Logical OR (||)


The logical OR (||) operator (logical disjunction) for a set of operands is true if and only if one or more of its operands is true.
Example:

var a = 30;
var b = -20;

console.log((a < b) || (b > 0) || (b > 0));  //false

Logical NOT (!)

The logical NOT (!) operator (logical complement, negation) 

Example:


var a = 30;
var b = -20;
console.log(!((a>0) || (b < 0)));   //false

5.String Concatenation(operators)

The concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings.

Example:


console.log("hello " +  "world");

var myName = "Ro";

console.log(myName + " coderes");

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
hello world
Ro coderes 

PS C:UsersThe-BeastDesktopjavascript Blog>

6.Conditional (ternary) operator

Expressions and operators -ternary operators

The conditional (ternary) operator is the only JavaScript operator that takes three operands.

Example1:


var age=18;
age=age>=18 ? "you can vote" : "you can't vote"
console.log(age)

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
you can vote
PS C:UsersThe-BeastDesktopjavascript Blog> 

Example2:


var age=16;
age=age>=18 ? "you can vote" : "you can't vote"
console.log(age)

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
you can't vote
PS C:UsersThe-BeastDesktopjavascript Blog> 

Questions

1.What will be the output of 3**3? 


console.log(3**3); // 3*3*3

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
27
PS C:UsersThe-BeastDesktopjavascript Blog> 

2.What is the Difference between == vs === ?

“==” He only checks the value, not the data types.

“===” He checks both data types and values. If both values ​​and data types are the same, then the return will be true.

Example:


var num1 = 5;    //number
var num2 = '5';  //string

console.log(typeof(num1));
console.log(typeof(num2));

console.log(num1 == num2 );

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
number
string
true  
PS C:UsersThe-BeastDesktopjavascript Blog> 

Example:


var num1 = 5;
var num2 = '5';

console.log(typeof(num1));
console.log(typeof(num2));

console.log(num1 === num2 );

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
number
string
false 
PS C:UsersThe-BeastDesktopjavascript Blog> 

Example:


var num1 = 5;
var num2 = 5;

console.log(typeof(num1));
console.log(typeof(num2));

console.log(num1 === num2

output:


PS C:UsersThe-BeastDesktopjavascript Blog> node index.js        
number
number
true  
PS C:UsersThe-BeastDesktopjavascript Blog> 

watch the video:https://youtu.be/KGkiIBTq0y0

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article C Programs Practicals
Next Article Control Statement and loops
3 Comments 3 Comments
  • Hung says:
    November 17, 2021 at 2:43 am

    Wow, that’s what I was exploring for, what a data! existing here at this
    web site, thanks admin of this site.

    Reply
  • Mahalia says:
    November 18, 2021 at 10:57 pm

    What’s up to every body, it’s my first pay a
    quick visit of this web site; this website includes
    remarkable and in fact fine information in favor of visitors.

    Reply
  • Bianca says:
    November 22, 2021 at 11:59 pm

    Greetings from Idaho! I’m bored at work so I decided
    to browse your site on my iphone during lunch break. I enjoy the info
    you present here and can’t wait to take a
    look when I get home. I’m shocked at how fast your blog loaded on my mobile
    .. I’m not even using WIFI, just 3G .. Anyhow, wonderful
    site!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?