Javascript 101 — Comparison & Conditions — 04

Melih Turhanlar
4 min readDec 31, 2021

Before reading that article you can also check Javascript-101 Embedding objects and arrays.

Comparison

In that text, we will explain much more comparisons. As it is in all programming languages javascript also compares two values with ==;

10 == 10;

this value will give us true. Because left values which is 10 and right one which is 10 are equals.

Same as strings;

“hello” == “hello”; will give us true

But “hello” == “Hello” will give us false, because javascript is case sensitive.

In our second text, we have talked about polymorphism. So now we will see here some examples of that issue.

10 == “10”; should it give false or true?

it gives true because there is polymorphism and strings have precedence so javascript takes the left integer value and changes the data type of it and compares it with the right string value.

If we want to use data type comparison then we should use not == but ===.

this time 10 === “10” will give us false.

Now let’s look not equal! In javascript, there is two not equal

  • first one !=,
  • the second one is !==.

!== with that sign, we can compare data types also. Here is the example

As in all languages in javascript >,>=,<,<= same. Here is the example.

if Condition

The syntax of the if condition in javascript looks like other programming languages.

Basic syntax of it:

if ( comparing ) {
execution;
}
elseif (comparing) {
execution;
}
else{
execution;
}

Now I’ll briefly tell about examples below.

We have defined three variables named FirstLesson, SecondLesson, and DoYouWantBreak.

In the first example, the student doesn’t want any break so he/she can continue the lesson!.
Our console prints “You can continue your lesson!

Here the student wants a break and its end of FirstLesson so the console prints out “You can have your first break for 10 minutes.

Here the student wants a break but it is not time for a break so the console prints out “Wait for the time!

For

For loop is also looks like other programming languages.
Let's examine our examples;

Here we have defines two arrays in order to use in our for loop.

for (define an index; compare it with some value; increment the index)

{
execute your commands here;
}

As it is seen we have defined i variable and attended to it 0. So every time our loop will increase i value and will ask is it less than listofsomewords.lenght or not?

Our console.log(listofsomewords1[i]) has printed all elements in the array. In for loop, when we have defined variable with var. It is in the scope of the Window Object.

As we see in the picture above we can reach it. If we do not want to define that variable as global we should use not var but let.

We can use without incrementing our i value and print all elements of the array. How?

With in it is possible to touch every element of the array. In our example, we have defined an index and with in in our array, we have printed all elements of our listofsomewords2 array.

Here is the output of our for loop.

--

--