INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

a funny take on understanding 'if statements' in javascript ?
3

a funny take on understanding 'if statements' in javascript ?

a funny take on understanding 'if statements' in javascript ?

(OP)
"If I earn at least $500 this week, I can pay the electric bill; I must pay the bill, or else my lights will be
shut off."


You can generate the exact same sentiment in JavaScript using only two variables: income and lights. Presume
that the variable income has been previously assigned some numeric value representing this week's earnings.

The variable lights will be assigned on the condition of the income and will receive a Boolean value (true or false)
because there are only two possible outcomes for the lights: remain on or shut off. Given all this, the JavaScript
version of the above statement, using the JavaScript If...else construction, would look like this:


if (income >=500) {
lights = true;
else
{ lights = false;
}


Given this statement, JavaScript would first evaluate the expression (income >= 500). If this returns a true result,
JavaScript executes the statements within the first set of brackets {}. If the expression is false, JavaScript skips
ahead to the else clause and executes the statements within the set of brackets immediately below the else
keyword. You can include as many statements as you want within the brackets of a clause, but you must
remember to end each statement with a semicolon (;) as in this example:

if (income >=500) {
lights = true;
savings=income-bills;


To JavaScript, the semicolon is the signal that one statement has finished and another is about to begin.


smile

RE: a funny take on understanding 'if statements' in javascript ?

Actually the semicolon is optional in javascript and a statement will also terminate with an end of line marker (/n) so a semicolon is only required when multiple statements are on a single line.


eg:
a=1;b=2;c=3;

is syntactically and operationally identical to

a=1
b=2
c=3

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: a funny take on understanding 'if statements' in javascript ?

Hi

Quote (Chris)

a semicolon is only required when multiple statements are on a single line
Generally correct. Just you can not count on the interpreter to consider the same thing a single line.

Try to remove any of the semicolons here :

CODE --> JavaScript

var str = 'test'

;['whatever'].forEach(function(value) { console.log(value) })

;(function() { console.log('whatever') })() 
( The strange placing of semicolons in such case is just a frequent habit. )

Feherke.
feherke.ga

RE: a funny take on understanding 'if statements' in javascript ?

Hi

Or in this extremely simple case, no ternary winky smile :

CODE --> JavaScript

lights = income >= 500 

To stick with the original example :

CODE --> JavaScript - enough money

var bill = 500
var income = 800

;(light = income >= bill) && (income -= bill)

console.log('light', light)     // light true
console.log('income', income)   // income 300 

CODE --> JavaScript - insufficient money

var bill = 500
var income = 400

;(light = income >= bill) && (income -= bill)

console.log('light', light)     // light false
console.log('income', income)   // income 400 

Feherke.
feherke.ga

RE: a funny take on understanding 'if statements' in javascript ?

Quote (Feherke)


Generally correct. Just you can not count on the interpreter to consider the same thing a single line.

Sure however, the only time I've seen it 'broken' is when the script document has been through some sort of "white space" compression/removal tool. It is of course good practice to include the semicolons particularly if you are going to "minify" [and what a stupid 'word' that is!] or obfuscate the scripts.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: a funny take on understanding 'if statements' in javascript ?

Hi

Why would need to alter whitespaces for that ? If you remove the semicolons from my example posted at 5 Aug 15 12:54, works for you ? For me it crashes both with Gecko and WebKit.

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close