πŸ“’
SDV503
  • SDV503
  • Command Prompt
    • Windows Command Prompt
    • Mac OS Terminal
  • GIT & GITHub
    • GitHub
    • Git
      • Git Workflow
        • Forking Workflow
  • README
    • How to write a readme.md file?
    • Write a better README.me
    • Generate a README for GitHub Repo Quickly
  • Code-comments
    • Clean Comments
    • Writing Comments in a Good way
  • Pair Coding
    • What is Pair Coding?
    • Pair Programming Experience
  • Programming?
    • What is Programming?
    • What Is A Programming Paradigm?
    • JavaScript Programming Paradigms
  • Number Systems
    • Decimal and Binary numbers
  • JavaSCript
    • Introduction To JavaScript
      • The JavaScript Engine
  • JS Call Stack
    • JavaScript call stack
    • JavaScript & Memory
      • Memory Leaks in JavaScript
    • Execution Context and Execution Stack in Javascript
      • Javascript Execution Context and Hoisting
  • JavaScript Variables
    • Introduction to JS Data Types
    • Primitive and Non-Primitive
    • Operator precedence and associativity
      • JS Operators Part One
      • JS Operators Part Two
      • JS Operators Part Three
    • Clean Code for JS Variables
  • JavaScript Scopes
    • Scope (Chain) Visualized
  • Javascript β€Šβ€”β€Šthis keyword
  • JavaScript Data Types
    • More JavaScript Data Types
  • JavaScript Expressions and Statements
  • if/else, switch and ternary operator
    • If/Else statement
  • Switch Statement
  • Ternary Operator
  • JavaScript Loops
    • Loops in JavaScript
      • Trouble With Loops
  • Objects
    • JavaScript Objects
      • Prototypal Inheritance Visualized
      • JavaScript Number Object
      • JavaScript String Object
  • Functions
    • JavaScript Function Part One
    • JavaScript Function Part Two
    • Immediately Invoked Function Expressions ~ IIFE
    • JS => Arrow Functions
    • JavaScript Callback
    • Hoisting in JavaScript
      • Hoisting Visualized
    • Recursion Functions
    • Curry and Function Composition
  • JavaScript Features
    • JSpread Operator
    • JavaScript Built-in Functions & Objects
  • Data Structures&Algorithms
    • JavaScript’s Data Types
    • Data Structures in JavaScript
      • Introduction to Big O Notation
      • Big O Notation in Javascript
      • Linked Lists
        • Linked Lists β€” 2
      • Hash Tables
      • Stack & Queue
  • TLDR
    • Single quotes (β€˜ ’) and double quotes (β€œ ”) in JavaScript
  • ES6
    • Generators and Iterators
    • Javascript Classes
    • JavaScript closures
    • JavaScript Promises & Async/Await
      • Event Loop Visualized
  • C#
    • What does C#? (C Sharp)
    • C# vs JavaScript
    • What Is The Difference Between C#, .NET, ASP.NET, Microsoft.NET, and Visual Studio?
    • What is the .NET Framework?
    • Methods and Properties of Console Class in C#
    • Datatypes in C#
    • C# Code Comments
    • The if statement
    • The switch statement
    • Loops
    • Comparison operators
    • Addition assignment operators
    • The String Interpolation Operator
    • Arrays
    • Lists
    • Dictionaries
Powered by GitBook
On this page
  • 4. Logical Operator
  • 1. OR ||
  • Short-circuit evaluation
  • Short-circuit evaluation example
  • Summary
  • 2. AND &&
  • && operator also follows the short-circuit evaluation method
  • Now, what is the difference between OR and AND .?
  • Summary
  • 3. NOT !
  • !! Double Negation
  • Summary

Was this helpful?

  1. JavaScript Variables
  2. Operator precedence and associativity

JS Operators Part Three

PreviousJS Operators Part TwoNextClean Code for JS Variables

Last updated 5 years ago

Was this helpful?

4. Logical Operator

Logical operators are used to compare values. Any type of values can be compared using logical operator.

Three logical operators in Javascript are:

  1. OR ||

  2. AND &&

  3. NOT !

Lets dive into it

1. OR ||

Represented with two pipe characters or two vertical lines and called as disjunction

||

The || operator represents

If any of the operand (argument) is true. It returns true, else it returns false

It means it returns false only if both arguments are false

If an operand or the operands are not boolean, then it is converted to the boolean for evaluation, and returns the original value of the operand, not the converted boolean. We know 1 represents true and 0 represents false. Here in Ex: 2, the numbers are converted to boolean in the operation, but not in the output.

Look at the example below. Ex : 3

What is happening ? Here its a combination of number and boolean

line 4 : no doubt, 0 is false. So it returns true
line 5 : 1 represents true, So it returns 1. no doubt
line 6 : both operands are true, but why does it return true, not 1 .? πŸ€”
line 7 : same like line 5, no doubt.
line 8 : Again, both operands are false, why does it return 0, not false.? πŸ€”
line 9 : no doubt.Small conflict in line 6 and line 8. right .? while comparing with boolean and number type. line 6 returns boolean value and line 8 returns number value. Why.? 

Short-circuit evaluation

The evaluation stops once the operator finds the truthy value.

true || false || 0

It evaluates the second operand, only if the first operand is a falsy value, same rule applies to the third operand. In the above example, even before checking the second operator, it returns the true. Operator does not even know what is after the first operand.

Literally the operation stops where it finds a truthy value

OR seeks the first truthy value

It evaluates from left to the right

If the first operand is true, it stops there and returns the original value of that operand

If every operands in the expression are false, it returns the last operand

Recheck: 
line 6 : true || 1  // first operand is true. It returns trueline 8 : false || 0 // first operand is false. Checks the second operand. It is also false. Returns the last operand 0  

As OR is seeking to find a true, it stops evaluation until it finds a true. If any value is true, it returns the first true value found. If all values are true, it returns the first true value. If all values are false it returns the last false value

Compare the line numbers to avoid confusion

0, null, undefined, empty string and false are false values. Positive integer, Negative integer, a string and true are true values

-1 is also a true value.

Short-circuit evaluation example

line 5 : operation stopped after finding true, So x not get assigned
line 6 : x is undefinedline 11 : operator found first operand is false, moves further. It understood need to assign 10 to the already declared variable y
line 12 : returns 10

Summary

  1. syntax: ||

  2. Returns true, if any one operand is true

  3. Returns false, only if every operands are false

  4. 0, false, empty string, null, undefined are false

  5. OR operator seeks first truthy value

  6. Operation stops once a value is evaluated as true, and return the original value

  7. If all values are evaluated as false, then it returns the last original value

2. AND &&

Represented with two ampersands and called as conjunction

&&

Returns true, only if both operands are true, else false

It means it returns false, if any argument is false

Four possible logical boolean combinations

Four possible logical number combinations

Only returns true, when all operands are true

&& operator also follows the short-circuit evaluation method

AND seeks the first falsy value

It evaluates from left to the right

If the first operand is false, it stops there and returns the original value of that operand

If every operands in the expression are true, it returns the original value of the last operand

As AND is seeking to find a false, it stops evaluation until it finds a false. If any value is false, it returns the first false value found. If all values are false, it returns the first false value. If all values are true it returns the last true value

line 4 : no false found, so returned the last original value, its 1
line 5 : no false found, so returned the last original value, its true line 6 : first operand is true, so moved further operand to check if its a falsy value, yes its a falsy value, so returned 0
line 7 : found first operand false, so no more evaluation required, returned 0Now you say, what is happening from line 11 to 14 .?Remember, AND seeks the first falsy value

Check the line numbers to avoid confusion

0, null, undefined, empty string and false are false values. Negative integer, positive integer, a string and true are true values.

Now, what is the difference between OR and AND .?

OR always returns true if any true exits, Only returns false if all arguments are false

AND always returns false if any false exits, Only returns true if all arguments are true

OR is a truth seeker like you, AND is a false seeker.

Summary

  1. syntax: &&

  2. Returns false, if any one operand is false

  3. Returns true, only if every operands are true

  4. 0, false, empty string, null, undefined are false

  5. AND operator seeks first falsy value

  6. Operation stops once a value is evaluated as false, and returns the original value

  7. If all values are evaluated as true, then it returns the last original value

3. NOT !

Represented with exclamation and called as Negation

!

Not is a unary logical operator, it means β€œNot” works with a single argument.

Returns inverse value

It means, if the argument is true, it returns false and if the argument is false, it returns true.

!! Double Negation

!!

It means true and !!true are equivalent

See how it works..

Summary

  1. NOT is a unary operator

  2. Returns the inverse value, if value is true, NOT returns false and vice-versa

  3. Double negation reverses the inverse

The && operator represents

The ! operator represents

logical and
logical not
logical or