JS Tutorial For Beginners – #2 If Else & Comparison Operators




In this free Javascript tutorial, we’ll cover if, else statements & comparison operators. This is a Free JS Course designed for beginners. These Javascript lessons cover the basics of Javascript and programming basics from the very beginning.

In the last lesson, we covered variables and several Javascript types. Before covering more types, we’ll cover comparison operators and if, else statements.

Comparison Operators Covered in this Video:
(youtube doesn’t allow me to do less-than and greater-than signs, which is really quite silly,
so I have to use LT and GT)
LT Less than
GT Greater than
LT= Less than or equal to
GT= Greater than or equal to
== equal to
!= not equal to

There are also “strict” equality types
=== same value and same type
!== not same value and same type

For example:
“2” == 2 will be true – even though they’re different types, they’re considered equal
“2” === 2 will be false because the second value is a number type, where the first is a string type
You should use the triple === whenever possible, as it’s more specific.

-~-~~-~~~-~~-~-
Also watch: “Responsive Design Tutorial – Tips for making web sites look great on any device”

-~-~~-~~~-~~-~-

Original source


39 responses to “JS Tutorial For Beginners – #2 If Else & Comparison Operators”

  1. alert(`(You have been pulled over by the police)`); var name = prompt(`What is your name?`); var lastname = prompt(`What is your last name?`); var age = prompt(`What is your age?`); alert(`The Police Officer asks for your ID`); if(age < 18) {
    var choice1 = prompt(`What do you do? 1.Make a run for it in your car and try to lose him 2.give him your ID and get arrested`)
    ;if (choice1 == 1) {
    alert(`you get caught and get sentenced to jail for an year`)
    };if (choice1 == 2){
    alert (`you get arrested and get sentenced to jail for an year`)
    }
    };if(age >= 18) {
    var choice2 = prompt(`What do you do? 1.Make a run for it in your car and try to lose him 2.give him your ID`)
    ;if (choice2 == 1) {
    alert(`you get caught and get sentenced to jail for an year (Why did you do that)`)
    };if (choice2 == 2){
    alert (`"You are free to go"`)
    }
    }
    spent a good half hour making this

  2. var name = prompt('name'); var age = prompt('age '+name); if (age < 18) {

    alert('you can't play this game '+name)

    } else if (age == 18) {

    alert('finally now you can play '+ name)

    } else {

    alert('yeah you can play this '+name)

    }

  3. cant seem to get this to work
    let spiderMan = "Good Guy"
    if (spiderMan = Good Guy) {
    alert("Spiderman Spiderman does whatever a spider can");
    } else alert {
    ("Beware of Venom");
    }
    I've tried == too, still gives Uncaught SyntaxError: Unexpected identifier

  4. var age=prompt("What's your age ?")
    if(age>18){
    alert("you are allowed to continue");
    }else if(age==18){
    alert("good now we can get down to buisness !");
    }else{
    alert("QUIT THIS WEBSITE YOU FILTHY !")
    }

  5. I never met any teachers who are explain very clearly any programs. You are the best. I like to study many things from you. How can I contact you sir. I am very greatful to you sir. Make many videos.thank you so much

  6. My attempt: A calculator that performs four basic operations i.e. addition, subtraction, multiplication and division.
    var num1=+prompt('Enter first number: ');
    var opr =prompt('Enter the operation : ');
    var num2=+prompt('Enter second number');
    if(opr=='+') {alert('result: '+(num1+num2))}
    else if(opr=='-') {alert('result: '+(num1-num2))}
    else if (opr=='*') {alert('result: '+(num1*num2))}
    else if(opr=='/') {alert('result: '+(num1/num2))}
    else {alert('Operation is not supported.')};

  7. I made a sign up system! 😀
    alert("Press ok to sign up")

    var Username = prompt("Username:");

    var Password = prompt("Password:");

    var ConfUser = prompt("Your username:");

    if ( ConfUser != Username ) {

    alert("Not correct username!");

    } else {

    alert("Correct Username")

    }

    var ConfPass = prompt("Your password:");

    if ( ConfPass != Password) {

    alert("Not correct password!")

    } else {

    alert("You have gotten accsess to top notch dank memes")

    }

  8. age=+prompt('Enter your age');
    if(age==16){
    alert('YAYYY YOU CAN FINALLY DRIVE!');
    }
    else if(age<16){
    alert('Do you wanna get arrested? Juvi isn't fun.');
    }
    else{
    alert('Drive saaaaaaafe.');
    }

    Thankyou so muchhh 🙂
    Your videos are AWESOME!

  9. Hey, a bit of help please? My code is

    var name=prompt("Enter Your First and Last Name, Please!"); var Birthday=prompt("Enter the year you were born"); var month=prompt("Enter The Month That Your Birthday Takes Place On In All Lowercase Letters!"); if(month='january') {month=1}; if(month='february') {month=2}; if(month='march') {month=3}; if(month='april') {month=4}; if(month='may') {month=5}; if(month='june') {month=6}; if(month='july') {month=7}; if(month='august') {month=8}; if(month='september') {month=9}; if(month='october') {month=10}; if(month='november') {month=11}; if(month='december') {month=12}; if (month<=5) {Lel=0} else {Lel=1}; var Third=(2018-Birthday-Lel); alert('Your Name Is '+name+' And You Are '+Third+' Years Old').

    I'm trying to say the person's full name and age, but it always sets the month value to december, making it twelve. This, in turn, subtracts 1 from the Birthday variable, causing the person's age to appear younger than it actually is. Help!

  10. this was my version of the code, took me a second to figure it out but after seeing some comments of other people doing it i decided to do it myself.

    var age = prompt('how old are you'); if (age < 30) {
    alert('Youve got a long way');
    }; if (age >= 30) {
    alert('Sheesh your old')
    }

  11. LT Less than
    GT Greater than
    LT= Less than or equal to
    GT= Greater than or equal to
    == equal to
    != not equal to

    < Greater than
    > Less Than
    >= Less Than And Check
    <= Greater than And Check
    == Full Check
    != Doesnt Check

    the lower is what i was taught but do what you feel is right for you

  12. My homework-

    var day=prompt('Name the days of the week you want check in JAPANESE');
    if (day=='monday') {
    alert('getsuyoubi & ……Mr Will is the best!');
    }
    else if (day=='tuesday') {
    alert('kayoubi & ……Mr Will is the best!');
    }
    else if (day=='wednesday') {
    alert('suiyoubi & ……Mr Will is the best!');
    }
    else if (day=='thursday') {
    alert('mokuyoubi & ……Mr Will is the best!');
    }
    else if (day=='friday') {
    alert('kinyoubi & ……Mr Will is the best!');
    }
    else if (day=='saturday') {
    alert('doyoubi & ……Mr Will is the best!');
    }
    else if (day=='sunday') {
    alert('nichiyoubi & ……Mr Will is the best!');
    }
    else {
    alert('Javascript ROCKS!');
    }
    ….Damn me(learning Japanese while practicing JS)!

  13. try this
    var hi = prompt('how old are you ?');
    if (hi > 13) {
    alert('wait a moment , just click ok …..'); var number1 = prompt('when where you born? years '); var number2 = prompt('what mounth wher you born?'); var number3 = prompt('and day?'); var name = prompt('whats youre first name'); var name2 = prompt('whats your last name?'); alert('hi there'+' '+','+' '+name+' '+name2+" "+'you were born in'+' '+number1+' '+number2+' '+'on the'+' '+number3+'th');

    if (number1 < 1999 ) {
    alert('bitch you too old');
    } else {
    alert('welcome to our site')
    }

    if ( number1 > 2003 ) {
    alert('damn too young');
    } else {
    alert('welocme to our site');
    }

    } else {
    alert('too young goodbye');
    }

Leave a Reply