220 likes | 319 Vues
This article covers the basics of JavaScript conditions using the if-then-else syntax. It explains how to evaluate boolean expressions and illustrates practical examples, such as checking age for retirement eligibility. You'll learn how to utilize relational operators, combining conditions with AND and OR statements. The tutorial also walks through form fields, including checkboxes and radio buttons, demonstrating how JavaScript interacts with HTML elements to enhance user experience. Perfect for beginners looking to grasp conditional logic in programming!
E N D
if (then) else Syntax if ( ) Boolean expression { instructions if expression is true (then) } else { instructions if expression is false }
Example Check age < 59 >=59 and <65 >65 Gotta Work! Early Retirement? Retire!
Example – Step 1 if ( age < 59 ) { alert(‘Gotta Work!’) }
if ( age < 59 ) if ( age >= 59 && age < 65 ) Example – Step 2 { alert(‘Gotta Work!’) } else { (age >= 59) && (age < 65) { alert(‘Early Retirement?’) } }
if ( age < 59 ) if ( (age >= 59) && (age < 65) ) Example – Step 2 (v2) { alert(‘Gotta Work!’) } else { alert(‘Early Retirement?’) }
if ( age < 59 ) if ( (age >= 59) && (age < 65) ) Example – Step 3 { alert(‘Gotta Work!’) } else { alert(‘Early Retirement?’) } else { alert(‘Retire!’) }
if ( age < 59 ) if ( (age >= 59) && (age < 65) ) Example – Step 3 (v2) { alert(‘Gotta Work!’) } else { alert(‘Early Retirement?’) } else { alert(‘Retire!’) }
Relational Operators Equal to == > Greater than < Less than >= Greater than or equal to <= Less than or equal to != Not equal to
Danger Will Robinson function check(p) { var a = p alert('initial a = ' + a) if (a = 5) { alert('if a = 5') } alert('final a = ' + a) } a == 5 JavaScript assignment statements execute and evaluate to true. check(10) initial a = 10 if a = 5 final a = 5
Boolean Expression if ( (age >= 59) && (age < 65) ) Condition 1 Condition 2 Expressions and Conditions
AND and OR AND && if (condition1 && condition2) Both conditions true expression is true. Otherwise expression is false. OR || if (condition1 || condition2) Either condition true expression is true.
<input type="checkbox" name="student" checked="checked" /> Checkbox
Checkbox document . form . field . checked document.getstuff.student.checked Boolean true or false
Index = 0 Index = 1 <input type="radio" name="sex" id="male" value="M" /> <input type="radio" name="sex" Id="female" value="F" /> Radio Button Gender: Male <input type="radio" name="sex" id="male" value="M" /> Female <input type="radio" name="sex" id="female" value="F" />
var message var indx message = ‘by Index using Checked property\n‘ for (indx = 0; indx < document.getstuff.sex.length; indx++) { message = message + 'Indx=' + indx + ' checked=' + document.getstuff.sex[indx].checked + '\n‘ } alert(message)
message = ‘by Index using Value property\n‘ for (indx = 0; indx < document.getstuff.sex.length; indx++) { message = message + 'Indx=' + indx + ' value=' + document.getstuff.sex[indx].value + '\n‘ } alert(message)
message = ‘by d.f.id.checked\n‘ message = message + 'document.getstuff.male.checked=' + document.getstuff.male.checked + '\n‘ message = message + 'document.getstuff.female.checked=' + document.getstuff.female.checked + '\n‘ alert(message)
<select name="major"> <option value="mis">Management Information Systems</option> <option value="cis">Computer Information Systems</option> <option value="isp">Information Security and Privacy</option> </select> Select and Option
Select and Option document . form . field . value document.getstuff.major.value <select name="major"> <option value="mis">Management Information Systems</option> <option value="cis">Computer Information Systems</option> <option value="isp">Information Security and Privacy</option> </select>