First of all, let me just apologise for the blog title layout..
How else was I going to grab your attention and help you to understand Operators and Conditional Statements?
This is my 4th blog post on DEV, I am beginning to write about what I have learned, or gone over again, as it really helps me to learn and embed the fundamentals, whilst also helping others on their journey!
else statements: If we decide to have a coffee, then we would make one and enjoy, if not we would opt for the tea. A conditional statement checks certain conditions, and then executes the code accordingly based on these conditions.
Firstly, lets talk about
IF statements, think of these as the top layer of a cake, and as we dive into
if else statements - these will become our other layers..
if statement below:
Its completely ok to be confused as you begin learning about
conditional statements - They are quite tricky to learn to begin with, the
else if statement always comes before the else statement (This is our reliable statement.. You will find out why when you reach that section!) These statements are the middle layers of our cake, you can add as many of them as you like, to make your code as easy, or as complex, as you'd like. This is an example of an
else if statement:
Actually yes, there is..
else statements, these are added to an
if statement, so that if the condition within an
if statement, resolves to
false then this block of code will execute instead - We could call it a reliable statement (should all your previous code render
false- you know you have always got a statement to fall back on!)
Here's an example:
There are three types of operators we can use for
Conditional statements These are:
&& - The and operator: This operator checks that both conditions are true. when using this operator, both statements must be
true resulting in the block executing and therefore printing to the console, however, if either condition is
false the condition will overall render
false and result in execution of the
|| - The Or operator: This operator allows for a bit more flexibility in conditions, it allows for any of the statements within a code block to be
true in order to successfully execute and print to the console.
(!) - The Not operator: Also known as the bang operator, this takes a
true value, and passes back a
false value - to put it simply, it returns the opposite of what is intended to be printed to the console.
Below is an example of Logical operators in action:
These are pretty self explanatory, and quite straightforward to use in conditional statements, there are 6 types of this operator.
These work by comparing the values from left to right.
< - Less than
> - Greater than
<= - Less than/or equal to
>= - Greater than/or equal to
=== - Is equal to
!== - Is not equal to
In this section Im going to talk about
truthy or falsy values So we have talked about
boolean values, so what about
non-boolean types? Good question!
Sometimes you might just want to check if a particular variable exists, but not actually assign it a value.
So let's look at
Non-boolean types include:
Take the above
conditional statement - the
if statement will execute as the of the variable slicesOfPizza will return true as it has been given a non-falsy value.
Which values are falsy? Lets find out!
Empty stringslike "" or ''
nullwhich means there is no value at all
undefined- when a variable isnt given a value
Not a Number
The second statement will return
false as it's value is an empty string! Yipee - Looks like your getting that dog!
Hooray for that particular empty string huh?
Pssst, If you bring together what you know so far about
truthy variable to the value if you use the
or operator which if you remember is the
else if statements are great if we want to check more than one condition. - Thats great but surely there must be a shorter way to write these condtions you say?
Your totally right - they are called
switch statement give us an alternative syntax than we can use to check multiple conditions and keeps our code looking clean and readable, helping us identify bugs or syntax errors quickly to resolve them.
break keyword instructs our computer to stop running the code block and to exit, meaning the computer wont continue to check for any more cases or execute any futher code within that block, without the
break keyword the code will continue to run without breaking, meaning all blocks of code will run regardless or wether they are correct.
This is just a basic summary on conditional statements and operators, it is always best to keep practising in order to understand these, but articles definitely help too!
If you made it this far... Thank you so much!
Follow me on Twitter: @miss_lorsx for updates on new blog posts coming soon!