DEV Community

Code_Regina
Code_Regina

Posted on

|JavaScript| JavaScript: Decision Making

          -Comparison Operators
          -Equality: Triple vs. Double Equals
          -Console, Alert, and Prompt
          -If Statements
          -Else-If
          -Else
          -Nesting Conditionals
          -Truth-y and False-y Values
          -Logical AND 
          -Logical OR
          -Logical NOT 
Enter fullscreen mode Exit fullscreen mode

Comparison Operators

is greater than
< is less than
= is greater than or equal to
<= is less than or equal
== is equality
!= is not equal
=== is strict equality
!== is strict non-equality

Equality: Triple vs. Double Equals

== double equals checks for equality of value, but not equality of type.
It puts both values to the same type and then compares them. Double equals does not care if the values are different types.
This can lead to some unexpected results.

1 == 1 is true
1 == '1' is true
Both integer 1 and string value '1' are the same for double equals even though they are two different data types. Double equals will convert the difference type and make them the same.

=== triple equals does care about type and therefore would render
1 == '1' as false.

Console, Alert, and Prompt

console.log() will print arguments to the console.

alert("This is a POP-Up Message")
Enter fullscreen mode Exit fullscreen mode

prompt("please enter a number")

Enter fullscreen mode Exit fullscreen mode

prompt is similar to alert except it will provide a text area to enter information.

If Statements

Conditionals is making decisions with code.

If Statement will only runs code if given condition is true


let rating = 3; 

if (rating === 3) {
  console.log("You are here!"); 
}

Enter fullscreen mode Exit fullscreen mode

If the condition is false then nothing happens.

Else-If

Else If will only run if not the first thing, maybe the other thing??


let rating = 2; 

if (rating === 3) {
  console.log("You are here"); 
}
else if (rating === 2) {
  console.log("Maybe over here too?"); 
}

Enter fullscreen mode Exit fullscreen mode

If the first part is false, then the other code might be right.

Else

Else will run if nothing else was true.
It is a last resort solution to the problem.


let rating = -99; 

if (rating === 3) {
console.log("You are here"); 
}
else if (rating === 2) {
console.log("Maybe over here too"); 
}
else {
console.log("You were nowhere to be found"); 
}

Enter fullscreen mode Exit fullscreen mode

Nesting Conditionals

Nesting
It is possible to nest conditionals inside conditionals.

We ask the end user these questions:
Password must be 6+ characters
Password cannot include space


let password = "cat dog"; 
if (password.length >= 6) {
  if (password.indexOf(' ') !== -1) {
 console.log("Password cannot include spaces"); 
}
else {
 console.log("Valid password!!"); 
}
}
else {
console.log("Password too short!"); 
}

Enter fullscreen mode Exit fullscreen mode

It is useful to test conditionals inside other conditionals.

Truth-y and False-y Values

All JS values have an inherent truthyness or falsyness about them.

Falsy values:
-false
-0
-""(empty string)
-null
-undefined
-NaN
Everything else is truthy

Logical AND

AND when both sides must be true, for the entire thing to be true.


1 <= 4 && 'a' === 'a'; 

9 > 10 && 9 >= 9; 

'abc' .length === 3 && 1 + 1 === 4; 

Enter fullscreen mode Exit fullscreen mode

Logical OR

OR If one side is true, the entire thing is true.


1 !== 1 || 10 === 10 

10/2 === 5 || null 

0 || undefined 

Enter fullscreen mode Exit fullscreen mode

Logical NOT

!expressions returns true if expression is false


!null 

! (0 === 0)

! (3 <= 4) 

Enter fullscreen mode Exit fullscreen mode

Top comments (0)