Expressions & operators


Equality operators


Equality: ==

console.log(1 == 1);
// true

console.log('1' == 1);
// true

Strict equality: ===

console.log(1 === 1);
// true

console.log('1' === 1);
// false

Iequality: !=

console.log(1 != 2);
// true

console.log(1 != '1');
// false

console.log(1 != '1');
// false

console.log(1 != true);
// false

console.log(0 != false);
// false

Strict iequality: !=

console.log(1 !== 2);
// true

console.log(1 !== '1');
// true

console.log(1 !== '1');
// true

console.log(1 !== true);
// true

console.log(0 !== false);
// true

Relational operators


Greater than operator: >

console.log(3 > 2);
// true

console.log(1 > 5);
// false

Greater than or equal operator: >=

console.log(3 >= 2);
// true

console.log(5 >= 10);
// false

console.log(7 >= 7);
// true

Less than operator: <

console.log(3 < 2);
// false

console.log(1 < 5);
// true

Less than or equal operator: <=

console.log(3 <= 2);
// false

console.log(5 <= 10);
// true

console.log(7 <= 7);
// true

Example

Write a function called getRelationship(x,y), which should return a string representing whether x is >, < or = y. The function parameters sould be numeric data types in the double-precision 64-bit floating point format (IEEE 754).

function getRelationship(x, y) {

    // Expecting 'number' type
    let isNumberX = true;
    let isNumberY = true;

    // x is not a 'number' type or has a value of NaN
    if ((typeof x != 'number') || isNaN(x)) {
        isNumberX = false;
    }

    // y is not a 'number' type or has a value of NaN
    if (typeof y != 'number' || y == NaN) {
        isNumberY = false;
    }

    // x and y are 'number' types
    if (isNumberX && isNumberY == true) {
        if (x > y) {
            console.log('>');
        }
        else if (x < y) {
            console.log('<');
        }
        else {
            console.log('=');
        }
    }
    // x is not a 'number' type or has a value of NaN
    // y is a 'number' type
    else if (isNumberX == false && isNumberY == true) {
        console.log('Can't compare relationships because ' + ''' + x + ''' + ' is not a number.');
    }
    // y is not a 'number' type or has a value of NaN
    // x is a 'number' type
    else if (isNumberX == true && isNumberY == false) {
        console.log('Can't compare relationships because ' + ''' + y + ''' + ' is not a number.');
    }
    // x is not a 'number' type or has a value of NaN
    // y is not a 'number' type or has a value of NaN
    else {
        console.log('Can't compare relationships because ' + ''' + x + ''' + ' and ' + ''' + y + ''' + ' are not numbers.');
    }
};

console.log(getRelationship(1,4));
// <

console.log(getRelationship(1,1));
// =

console.log(getRelationship('that',2));
// Can't compare relationships because 'that' is not a number.

console.log(getRelationship('this','something else'));
// Can't compare relationships because 'this' and 'something else' are not numbers.

console.log(getRelationship(3));
// Can't compare relationships because 'undefined' is not a number.

console.log(getRelationship('hi'));
// Can't compare relationships because 'hi' and 'undefined' are not numbers.

console.log(getRelationship(NaN));
// Can't compare relationships because 'NaN' and 'undefined' are not numbers.

console.log(getRelationship(NaN, 2));
// Can't compare relationships because 'NaN' is not a number.

console.log(getRelationship(NaN, undefined));
// Can't compare relationships because 'NaN' and 'undefined' are not numbers.

Falsy & truthy values


Primitives

JavaScript has the following primitive data types:

  • string
  • number
  • boolean
  • null
  • undefined
  • symbol (ECMAScript 2015)

To determine the type of an operand we can use the typeof operator:

typeof(false);
// 'boolean'

typeof(0);
// 'number'

typeof(undefined);
// 'undefined'

typeof(NaN);
// 'number'

typeof(Infinity);
// 'number'

typeof([]);
// 'object'

typeof({});
// 'object'

typeof('mario');
// 'string'

typeof('');
// 'string'

typeof('');
// 'string'

Falsy values

The following examples will translate to false:

if(false) {}
if(0) {}
if('') {}
if('') {}
if(null) {}
if(undefined) {}
if(NaN) {}

Truthy values

The following examples will translate to true:

if(true) {}
if(1) {}
if(0.2) {}
if(-0.2) {}
if('mario') {}
if('mario') {}
if(Infinity) {}
if(-Infinity) {}
if([]) {}
if({}) {}

Tricky examples

if(null == undefined) {} // true
if([] == true) {} // false
if(false == null) {} // false
if(NaN == NaN) {} // false
if(Infinity == true) {} // false
if(-Infinity == true) {} // false
if(null == undefined) {} // true
if(0 == '') {} // true
if(false == 0) {} //true
Ad1
advertisement
Ad2
advertisement
Ad3
advertisement
Ad4
advertisement
Ad5
advertisement
Ad6
advertisement