JavaScript for Beginners #7 - If, Else If, Else

Tech With Tim · Beginner ·🧠 Large Language Models ·6y ago

Key Takeaways

This video tutorial covers the basics of if, else if, and else statements in JavaScript, demonstrating how to use these conditional statements to execute code based on user input and change the color of text on a webpage. The tutorial provides a step-by-step guide on how to create and use these statements, including how to handle multiple conditions and optimize code efficiency.

Full Transcript

hello everybody and welcome back so in this video we're gonna be talking about if else if and else now these are ways to essentially check a condition before executing a certain block of code and this is very useful and what a lot of you guys have been asking about in the comments or that I've realized but like okay well what if I only want to run this piece of code if this is true or what if I want to do this and if this doesn't happen I do that well this is where we get into if else if and else which are extremely useful and kind of fundamental skills of the language so what I've done so far is I've actually just changed this example a little bit so what we have now is hello we have a text box and then we have a button that says click the reason I've done this is because what I want to do for this example is has it habet so the user can type something into this text box click click and based on what they type in we're actually gonna change the color of this field so we could change you know if they type in the word green this will change to green when they press the bug they type in blue this will change to blue when they press the button that's what I want to try to do so let's try to do that now the first thing we need to actually do this is an if statement so I'm gonna run through how an if statement works essentially the syntax for an if statement is you type the word if which is a keyword it'll highlight hopefully in this red color for you guys if you're in sub line then you have these brackets like this you put some kind of condition inside of these brackets and if this condition is true then whatever is inside of these curly braces will run if it's not true you're simply gonna skip over this entire if statement so everything that's inside of this block and go to the next line and just ignore it and not even bother doing it so for instance if I say if true well this will always run because obviously true is always true but you know that's a condition I can put that in here and that means essentially if I did console dot log something in here that would always run whereas if I put this to be false like that and then I was gonna console dot log this would never run because well this is false so let's actually give this a go and do something a little bit more advanced so in a real condition inside of here and then we'll try to do something so what I'm gonna do is actually grab the text that the user typed into my input box and just store that in a variable to start so I'm gonna save our text equals and in this case I guess it's document dot get oops element if I could type properly early in the morning for me here guys ID then we will do I and P which is the input box dot value okay so there we go that's gonna grab our text for us now what I'm simply gonna say is if text equals equals and I guess we'll say read will change the color of our header tag here to be read so to do that I'm gonna say document dot get element by ID p-- dot style dot value equals red and I guess our ID is gonna be heavy like that okay now the only issue is I need to put this inside of a function so that essentially whenever my button is pressed we can activate that function and see what the current text value is so to do that what I'm gonna say is function pressed like this then I'm gonna just close my bracket here and indent all this so there we go so I know it went kind of fast there but essentially what I've done is create a function called pressed this function will be clicked or you know activated when we press this button here then we will get whatever we've typed as a text field check if it's equal to read if it's read we will change the color otherwise we won't do anything so let's run this now it's save and refresh the page so here notice when I click click nothing's happening when I type hi you know nothing's happening but if I type red then it changes hmmm alright so I realized I've done style dog value rather than style dot color so that would probably be why that wasn't working per second what I tried it out so I need to change that to color let me show you again now so I think I showed if I type something like Hello obviously nothing's gonna happen but if I type red and click click after this has been refreshed then that will change to red and that is because we have this style dot color equals red and if tax is equal to red that will happen ok awesome but what if when I press the button and if they didn't type in red for example I want it to automatically change the color to black so maybe you know now if I start typing hi well it's still the color because it's not being changed back to the other color so how do I do that well this is where we can use something called an else now an else essentially as happens anytime this if statement doesn't happen so whenever you have an if/else block like this you know that either the if is gonna run or the else is gonna run and it's pretty easy to read it out you say if text equals red do this otherwise or else do whatever is inside the else block and that's as easy as Isis so I'm just gonna copy this line and change it so that this makes the color block like this and now let's run this and just show you how this works and that should hopefully help you so we have hello now let's type you know you obviously block we're not getting anything let's type red that changes to red and then if I type I don't know no we get it back to black so that is how this works if it's not red and then it changes color to block if it's red then it changes the color to red okay so what if we want to be able to change the color to red or blue and then anything else goes to black well how would I do that well this is where we use what's called an else if now this so far I hope makes sense again you just have if if this condition happens whatever's inside of here you do this otherwise you do this now the else if is kind of an extension on top of this and I'll talk about how it works in a second but let's get the syntax down first so essentially the elsif is kind of a combination between both the else and the if now this is essentially saying if this doesn't happen so if this if statement doesn't run we will check if the condition we put here is true if that's true we'll do whatever is inside of our brackets here otherwise we'll come down and we'll do what's below here so this actually means we can have multiple else ifs which we'll get into in a second let's do another example here where we make this say maybe green and then we'll just get the element ID and change it to the color green so let's tap this in like that and go green alright so again the way this we're operating here is we're gonna start by checking if the tax is equal to red if it is boom we go red we don't even bother reading the rest of this we don't need to do it okay if it's not red what we'll check if it's green if it's green we'll change the color to green awesome there we go we're done we don't even read the else if this is not true and this is not true so both above the else aren't true then we'll run the else and we'll change the color to black and that is pretty much how this works it's fairly straightforward so let's refresh let's type green there we go it goes green let's type red it goes red and let's type blue and obviously it goes to black now if I want to incorporate blue as well what I could do is simply make another else--if like this I think I need to actually do that yes that should work let's put another condition here say text equals equals equals that should actually just be three let's say blue and then what we can do is simply copy this and change the color to be blue and this should work for us now again this is gonna work because we can have as many else ifs as we want we can do you know if else if else if we don't even need to include this else if we don't want to I can get rid of that I can have just one else if the combination of these is fine just know that if you have an else if or if you have an Allison there needs to be the initial if to start that statement so let's look at this now and let's try this so let's refresh if I type blue we change to blue if I type red we go to red if I type green we go to green and if I type something random nothing happens because that else statement is no longer there and that is you know pretty much how that works now a lot of people get confused with this try to do some examples we'll do some more as we go through the different videos but just remember you start with your if statement if whatever happens here happens is true then this runs if it doesn't we'll check all of the else ifs if any of those are true we'll execute that and we'll stop looking for the rest of them otherwise we will do any else statement that is at the bottom and that else statement will happen no matter what so long as you know the ones above it were false now we can definitely have more than one if statement like there's different ways to do this for example right now I'm doing else ifs but what I could actually do is just make all these ifs and everything's actually gonna operate the same and the reason that's going to happen is because well you know in theory none of these conditions will be true at the same time right the tax can never be equal to green blue and red at the same time but the reason we might not want to do this in some instances is well like if it's red what's the point of checking if it's green right so in this case we have three if statements these are gonna happen and they're all gonna be checked every single in of these if statements every time we run this condition will be evaluated and we'll check if this needs to happen but the thing is if the statement is red like if we check the text and it's red what's the point of even bothering to check these other two if statements well that's what we put them in else and because we're saying well you know if it's red there's no chance that this condition will be true so there's no point in even trying to evaluate it right but you know we can do 3-up ifs like this I'll show you this works if i refresh and I go green that's fine and the way this is gonna work is you know we read this if statement we check it if it's true we do it then we read this if statement if it's true we do it like in theory we could make these true like I can make this statement true and now let me actually show you how this works if I type in let's say here so let's have a look at our code we have red true blue right so if I type red notice that we actually change to the color green now the reason that happened is because this is true right so this is always gonna run so we check this even though it actually changes this to red it gets changed to blue directly after but now watch what happens if I type blue well blue is actually run well why is that because this happens we change it to green and then we change it to blue afterwards so we get to see the blue so anyways that has been if else if and else I hope that makes sense as always if there's any questions leave them down below and with that being said if you guys enjoyed leave a like subscribe to the channel and I will see you guys in the next JavaScript tutorial

Original Description

This JavaScript tutorial for beginners covers if , else and else if statements. These can be used to execute code based on conditions that occur. In this video we run through an example where we change the color of text based on the users input. Playlist: https://www.youtube.com/watch?v=ykoxwrm0Seo&list=PLzMcBGfZo4-njtc5xy3qli4cN2zlKsoxd ◾◾◾◾◾ 💻 Enroll in The Fundamentals of Programming w/ Python https://tech-with-tim.teachable.com/p... 📸 Instagram: https://www.instagram.com/tech_with_tim 🌎 Website https://techwithtim.net 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/pr2k55t 📝 LinkedIn: https://www.linkedin.com/in/tim-rusci... 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 💵 One-Time Donations: https://www.paypal.com/donate/?token=... 💰 Patreon: https://www.patreon.com/techwithtim ◾◾◾◾◾◾ ⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡ Tags: - Tech With Tim - JavaScript for beginners - JavaScript If, Else, Else If - If, Else, Else If JavaScript - JavaScript Tutorial for Beginners #JavaScript #JS
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video tutorial teaches beginners how to use if, else if, and else statements in JavaScript to control the flow of a program based on conditions. It provides a step-by-step guide on how to create and use these statements, including how to handle multiple conditions and optimize code efficiency. By the end of this tutorial, viewers will be able to create conditional statements in JavaScript and handle user input to execute code based on conditions.

Key Takeaways
  1. Create an if statement with a condition and execute code inside curly braces
  2. Grab user input from a text box and store it in a variable
  3. Check if the user input is equal to a specific value and change the color of a header tag
  4. Put the code inside a function to handle button click events
  5. Use multiple else if statements to check for multiple conditions
  6. Use an else statement to execute code if none of the conditions are true
💡 The order of if, else if, and else statements matters and can affect the outcome of the program. The program will execute the first condition that is met and then stop checking the rest of the conditions.

Related Reads

📰
Teaching a 0.6B LLM to Rank: Score-Weighted BPR Fine-Tuning from Blended Relevance Signals
Learn to fine-tune a 0.6B LLM for search ranking using score-weighted BPR and blended relevance signals
Medium · Machine Learning
📰
Generative AI vs AI Agents vs Agentic AI: What’s the Real Difference?
Learn the key differences between Generative AI, AI Agents, and Agentic AI to better understand their applications and potential
Medium · AI
📰
Python Is Quietly Becoming the Operating System of AI
Python's rising popularity in the TIOBE Index indicates its growing importance in AI development, making it a de facto operating system for AI
Medium · AI
📰
Python Is Quietly Becoming the Operating System of AI
Python's rising popularity in the TIOBE Index signifies its growing importance as a foundational element in AI development, making it akin to an operating system for AI
Medium · Machine Learning
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →