×
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!
• Students Click Here

*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.

Students Click Here

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

 Forum Search FAQs Links MVPs

## 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.

### 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 ?

and there is always the ternary...

#### CODE

lights = (income >= 500)?true:false;
depending on the requirements of the test and assignment

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

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

Hi

Or in this extremely simple case, no ternary :

#### 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

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

nice one feherke!

Though I would write it ...

#### CODE

lights = (income >= 500);

To ensure precedence with parentheses, I also find it a little easier to read.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

#### 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!

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!

Already a member? Close this window and log in.