27.4 C
Gujarat
HomeJavascriptExpressions and operators

Expressions and operators

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 ARTICLES

3 COMMENTS

  1. 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!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

15,000FansLike
5,000FollowersFollow
535FollowersFollow
- Advertisment -spot_img

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Most Popular