JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • Comparison Operators
  • File Location
  • Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 5-Expressions and Operators

Comparison

One of the most useful functions of any coding language is the ability to compare two or more values and return whether or not they are equal. Almost every conditional statement you write will make use of these comparison operators.

Comparison Operators

In JavaScript, data-type does not usually matter when comparing two values. Unless specifically stated otherwise, the values are converted to the same type at the time of comparison.

Name

Operator

Description

Example

Equal

==

returns true if the values are equal, regardless of data-type

3 == 3 //true; '3' == 3 //true

Not Equal

!=

returns true if the values are not equal, regardless of data-type

3 != 4 //true; '3' != 3 //false

Strict Equal

===

returns true ONLY if the values are equal AND the same data-type

3 === 3 //true; '3' === 3 //false

Strict Not Equal

!==

returns true if the values are not equal OR not the same data-type

3 !== 4 //true; '3' !== 3 //true

Greater than

>

returns true if value A is larger than value B

4 > 3 //true; 3 > 4 //false

Greater than or equal

>=

returns true if value A is larger or the same as value B

4 >= 3 //true; 4 >= 4 //true; 3 >= 4 //false

Less than

<

returns true if value A is smaller than value B

3 < 4 //true; 4 < 3 //false

Less than or equal

<=

returns true if value A is smaller or the same as value B

3 <= 4 //true; 3 <= 3 //true; 4 <= 3 //false

AND

&&

returns true ONLY if value/condition A AND value/condition B are true

true && true //true; true && false //false

OR

||

returns true if EITHER value/condition A OR value/condition B are true

true|| false //true; false|| false //false

File Location

We will be working in the following file:

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
            └── 2-ControlFlow-and-ErrorHandling
            └── 3-Loops
            └── 4-Functions
            └── 5-Expressions-and-Operators
                01-assignment.js 
                02-comparison.js <----You will be working in this file.

Practice

  1. In comparison.js, create a variable and assign it a value.

  2. Use the comparison operators to compare the variable to other values and print the results.

  3. Use conditional statements and comparison operators to create a program with the following results:

If a person's age is over 25, they can rent a car. If a person's age is over 21, but less than 25, they can have a beer. If a person's age is over 18, but less than 21, they can vote. If a person's age is less than 18, make fun of them for being a child.

PreviousAssignmentNextTernary

Last updated 7 years ago