Google
Showing posts with label Operators. Show all posts
Showing posts with label Operators. Show all posts

Boolean Operators


Boolean Operators


Boolean operators help manage true and false values. They are most useful in if statements and the like, but they also have some interesting uses in and of themselves.

Logical NOT: !


The logical NOT operator turns a “truth” value into false and a false value into true. It’s another way of determining inequality. These two if statements operate identically :

if (myVar != true) {
// It's not true
}

if (! myVar) {
// It's still not true
}

It’s useful for determining that something isn’t true or that it’s missing.

var element = document.getElementById("myId");
if (! element) {
console.log("The element does not exist");
}

Logical AND: &&


The logical AND operator (&&) takes two operands. It returns the right operand only if the left operand is  truth.  If the left operand is false , it returns that value.

var tValue = 1;
var fValue = null;

true && tValue;   //   1

fValue && tValue;   //   null

The interesting thing about the logical AND operator is that it stops evaluating
and immediately returns if the left operand is “falsey.” Consider this example :

function myFunc() {
alert("executing myFunc!");
return true;
}

falseyValue && myFunc();  //  null

Because the left operand is “falsey,” the expression stops there and doesn’t execute myFunc(). In this case, you’ll never see the alert dialog box. This is useful for safeguarding yourself against potential errors. JavaScript will throw an error if you try to access a property on null.

var element = document.getElementById("doesNotExist");
element.nodeName;
TypeError: element is null
element && element.nodeName;
null
var element2 = document.getElementById("exists");
element2 && element2.nodeName;
"DIV"

Evaluating the nodeName property of a nonexistent element returns an error, but you can quickly determine whether the element exists with the logical AND operator.

Logical OR: ||


The logical OR operator (||) returns the left operand if it’s true, and the right operand otherwise. You can think of it as “Keep trying until you find the truth (or something like it).”

fValue || tValue;    // Second value
1
tValue || fValue;    // First value
1
false || fValue || anotherFalseValue || tValue;
1

Combining Boolean Operators


The fun part of Boolean operators comes from combining them to create complex logical expressions.

var element = document.body.firstChild;
if (element &&
(element.nodeName === "DIV" ||
element.nodeName === "SPAN") &&
element.childNodes.length) {
alert("All of these expressions are true");
}

Learning to read and write complex expressions like this is a challenge,
but it just amounts to understanding the smaller expressions one at a
time. Sometimes it helps to store some of the smaller expressions on
variables to enhance readability.

var isDiv = element && element.nodeName === "DIV"
var isSpan = element && element.nodeName === "SPAN";
var isDivOrSpan = isDiv || isSpan;
if (isDivOrSpan && element.childNodes.length) {
alert("All of these expressions are true");
}

Coding often requires balancing the number of lines and the readability of the code.


Ternary Expressions


Ternary expressions are a slightly more efficient way to manage values based on booleans. they let you write a basic if/else statement in one line of code.

result = expression ? "it's true" : "it's false";

This expression is equivalent to the following:

if (expression) {
result = "it's true";
} else {
result = "it's false";
}


operators

Operators are symbols, such as +, –, =, >, and <, that produce a result based on some rules. Data manipulated by an  operator called operands; for example, 5 and 4 are operands in the expression 5 + 4. Operators and operands create expressions. An expression combines a group of values to make a new value, x = 5 + 4.

operators can be categorized by number of operands :
operators manipulate two operands, are called a binary operators.
If there is only one operand, the operator is called a unary operator.
If there are three operands, it is called a ternary  operator.

 The operands can be either strings, numbers, Booleans, or a combination of these.

Precedence and Associativity

the following table is organized by operator precedence. 
The operators listed first have higher precedence than those listed last. 
with background color separating groups of operators at the same precedence level. 
The column labeled A gives the operator associativity, 
which can be L (left-to-right) or R (right-to-left), 
and the column N specifies the number of operands

Operator
Operation
A
N
++


- -


-

+

~

!


delete


typeof


void
Pre- or post-increment

Pre- or post-decrement

Negate number

Convert to number

Invert bits

Invert boolean value

Remove a property

Determine type of operand

Return undefined value
R


R


R

R

R

R


R


R


R
1


1


1

1

1

1


1


1


1






















*, /, %
Multiply, divide, remainder
L
2

+, -

+
Add, subtract

Concatenate strings
L

L
2

2



<<

>>


>>>
Shift left

Shift right with sign extension

Shift right with zero extension
L

L


L
2

2


2






<, <=,>, >=


<, <=,>, >=


instanceof

in
Compare in numeric order

Compare in alphabetic order

Test object class

Test whether property exists
L


L


L

L
2


2


2

2









= =

!=

= = =


!= =
Test for equality

Test for inequality

Test for strict equality

Test for strict inequality

L

L

L


L
2

2

2


2








&
Compute bitwise AND
L
2

^
Compute bitwise XOR
L
2

|
Compute bitwise OR
L
2

&&
Compute logical AND
L
2

||
Compute logical OR
L
2

?:
Choose 2nd or 3rd operand
R
3

=



*=, /=, %=, +=,
-=, &=, ^=, |=,
<<=, >>=, >>>=
Assign to a variable or property

Operate and assign
R



R
2



2





,
Discard 1st operand, return second
L
2



Example :

The order of associativity is from left to right. 
Multiplication and division are of a higher precedence than addition and subtraction, and addition and subtraction are of higher precedence than assignment. 
To illustrate this, we'll use parentheses to group the operands as they are grouped by JavaScript. In fact, if you want to force precedence, use the parentheses around the expression to group the operands in the way you want them evaluated. 
The following two examples produce the same result:

var r = 1 + 2 * 3 / 4;  // 2.5
 
could be written

var r = (1 + ( ( 2 * 3 ) / 4));  // 2.5
 
The expression is evaluated and the result is assigned to variable r.