if statements, explained.
What are if statements in programming?
Another important building block of any programming, regardless of programming language.
Every new programmer should therefore know about it. So what is it?
If statements are basically a way of saying:
I want you to do something, but ONLY on certain specific circumstances.
In other words, do something only if certain conditions are met.
Let's see an example:
Suppose you're writing a program. In that program, you're asking your user for their name so you can give them a personalised greeting:
var name = prompt("What is your name?")
console.log(`Hello, ${name}`)
You're also asking your user for their age. You want to tell your user "let's have a beer", but only if they're 18 years or older.
You can do this using "if" statements in your program.
An if statement (depending on language) looks something like this:
if(check){
// stuff to do if check passes
}
So in our example, we want to say:
If the user is 18 years old or older, invite them for a beer
The code in Javascript would look something like this:
var age = prompt("How old are you")
if(age >= 18){
console.log("Let's have a beer!")
}
If statements are otherwise called "conditional statements" because the if block is only executing when a certain "condition" is met.
Using if statements, you can therefore control the flow of your program, depending on certain criteria.
But what if we want to also do something if the check DOESN'T pass? Say for example, the user isn't 18 years or older?
This is where the else
comes in.
In our case, we want to tell the user "You're a bit too young to drink" if they're under 18.
This is what it looks like in Javascript:
var age = prompt("How old are you")
if(age >= 18){
console.log("Let's have a beer!")
}
else{
console.log("You're a bit too young to drink")
}
Thanks for reading this thread! 👋👋👋
Dev, Explained (43 part series)
- Javascript Scopes, explained.
- Javascript Promises, explained.
- Accessibility, explained.
- React, explained
- Should I use forEach() or map()?
- Should I use Flexbox or CSS Grid?
- Docker, explained.
- Unit testing, explained
- Git, explained.
- Typescript, explained.
- async/await, explained.
- The DOM, explained.
- Regular expressions, explained
- GraphQL, explained.
- Vue, explained.
- Svelte, explained.
- API, explained.
- Javascript Hoisting, explained.
- Immediately Invoked Function Expressions (IIFE), explained.
- ARIA roles, explained.
- Test-driven Development, explained.
- ARIA live regions, explained.
- aria-label in accessibility, explained.
- Type coercion in Javascript, explained.
- Variables, explained.
- if statements, explained.
- Arrays, explained.
- Currying in Javascript, explained.
- Memoization, explained.
- For loops, explained.
- Javascript Prototypes, explained.
- React Hooks, explained.
- Graph databases, explained.
- MongoDB, explained.
- Serverless, explained.
- Javascript Callback functions, explained.
- HTML, explained.
- CSS, explained.
- Responsive design, explained.
- Javascript, explained.
- The CSS Box Model, explained.
- CSS Flexbox, explained.
- CSS Grid, explained.