JavaScript comparison and how it works

Wednesday, 14th August 2019

Welcome again to another tech tutorial on javascript. In this javascript tutorial we are going to study how we can ensure that we compare our variables and any javascript data type effectively when try to run conditional statements.

What is javascript comparison statement?

Comparison statement is not applicable to javascript alone. Comparison statement is used in all programming language like php, python, java, ruby, go etc. its is used to effectively compare the difference between two variables or data supplied to be checked. See example below.

if(‘x’ == ‘y’){

//do something here

}

The above example is the simplest form of writing conditional statement on javascript and php.  The data supplied in this aspect is jus literal x and y which has no other meaning than a string.

To learn how to write perfect conditional statement in any programming language you must first have a solid understanding of the data types available in that programming language and how each of them function. In this article we are going to see the data types available in javascript after which we see how we can compare those data types and their values I our conditional statement.

Data types available in javascript

Javascript has six data types. These are the types of data value that you can use to write your code when programming in javascript. These data types are as well sync with javascript render engine such thst ot understands it and renders it according to the type which it belongs.

The data types we have in javascript include the foillowing

Objects evaluate to true

  • Undefined
  • Null
  • Booleans
  • Numbers
  • Strings

How javascript data types evaluate during conditional statement.

During our conditional statement see example below

const x = 12;

const y = 12;

if (x == y){

console.log(‘x is the same as y’);

}

Looking at the above example, if we run it on the browser you will see that the code actually logs ‘x is the same as y’ in our browser console.

Now lest dig deep into what just happened

Javascript sees that both varaibals holds the same data type and then goes further to compare their values which in that case is 12. Sonce both of them havs he sam,e values as 12 the conditional expression then evealuates to true.

If at any time the expression evaluates to false anything within that block will then be no longer processed.

You can read more on javascript if statement.

Lets look at another example

const myString = '';

const mySecondString = 'something;

if(myString == mySecondString){

                console.log('true');

}else{

                console.log('false')

}

//fasle

 

The abaove statement will return false despite the fact that both of them are of the same data type.

This is to let us understand that conditional statement evaluates both data type and data values before giving the final answer.

How condinal statement evaluates its params.

The following are the standard with which javascript conditional statement follows to evaluates any supplied arguments supplied to it. Its is vaery important that we knows this because this will aid us to write perfect conditional statements in javascript while maintain perfect code standard.

  • Objects evaluate to true
  • Undefined evaluates to false
  • Null evaluates to false
  • Booleans evaluate to the value of the boolean
  • Numbers evaluate to false if +0, -0, or NaN, otherwise true
  • Strings evaluate to false if an empty string '', otherwise true

Examples

  1. Objects evaluate to true

if ([0] && []) {

  // true

  // this evaluates to true because array a defined object in javascript even if its empty.

}

  1. Undefined evaluates to false

const x;

if(x){

                // false

                // Variables declared are not defined until they are assigned a value

}

  1. Null evaluates to false

const x = null;

if(x){

                // false

                // Variables declared with the value of null means that their value is not yet known so javascript forces this expression to evaluate to false

}

  1. Boolean evaluates to its value

const x =true;

const y = false;

if(x){

                // true

}

if(y){

                //false

}

if(!x){

                //false

}

 

if(!!x){

                //true

}

 

  1. Numbers evaluate to false if +0, -0, or NaN, otherwise true

if(0){

                //false

}

if(-0){

                //false

}

if(+0){

                //false

}

if(1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9   ){

                //true

}

In binary 0 is regarded as false and so not valid integer

 

  1. Strings evaluate to false if an empty string '', otherwise true

if(''){

                //false

}

if('string'){

                //true

}

Strings with no value evaluates to false because empty string is the same as nothing found. So empty strings evaluates to false.

Read the difference the  == and ==== equa eua and qua equa qua operator in javascript.

Leave a Reply
Total of 0 Comment