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”
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
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)
}
var answer = prompt("Nintendo or Microsoft?");
if (answer == "Nintendo") {
alert("smart man!");
}
else if (answer == "Microsoft") {
alert("oh you fool…");
}
keep driving safely, oh man hahahhhahh
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
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 !")
}
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
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.')};
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")
}
i need a answer, do I need a web page?
can anyone please explain how to do this thing using innerHTML..
Please Someone tell me how a 8 min video is just 5 mgb !!! with high quality this learning video has high quality with 8 minutes just 5 mgb please tell me some one Email me : learnareaa@gmail.com
this help me thx
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!
checkout this if conditions practice project ;
https://www.youtube.com/watch?v=mq081fut8v0&t=8s
var age = prompt('what is your age');
if (age < 18)
{
alert('sorry you can not enter this site');
}
else
{
alert('we are happy you are here!');
}
How to go in a next row? down arrow only moves a little bit on the right, but cant get in a next row without enter, but enter will invoke
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!
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')
}
from many channels something made me comfortable here to learn I have to say thank you
Im pretty sure that you cant drive with an apostothry
Math Teacher: and the sum of those two numbers is Blah, Blah, blah..
Student: o-o
other Student: Wow! math is my favorite subject!
You did the
if {
Command
} if {
Command
}
But what is the difference between using 2 if's. then if and else if?
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
really, little'er
For those in the UK
if (age < 18) {
alert ('you can not drink!');
} else if (age == 18) {
alert ('you can finally drink!');
} else {
alert ('you're probably an alcoholic!');
}
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)!
can someone tell me how to make nested if else statements in JS
once again!!! awesome!
he used the word "littler" and i tripped out. still tripping out lol
Thanks! these videos are great!
Just watched the first in the series. Excellent stuff – took me a while of wading through some terrible tuts before finding this gem!
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');
}
can you please make php tutorials like how to make a contact form and all that stuff
JavaScript for dummies…love it. Learnt a lot, thanks bruv
Welp….I'm confused.
Why not always use: " " to encapsulate strings?
Just curious: is there a difference in JavaScript of single quotes vs double quotes?
thanks dude