Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

c0deM0nK424

Programmer
Oct 28, 2007
126
GB
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]

 
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
 
Hi

Chris said:
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 :
JavaScript:
[b]var[/b] str [teal]=[/teal] [i][green]'test'[/green][/i]

[teal];[[/teal][i][green]'whatever'[/green][/i][teal]].[/teal][COLOR=orange]forEach[/color][teal]([/teal][b]function[/b][teal]([/teal]value[teal]) {[/teal] console[teal].[/teal][COLOR=orange]log[/color][teal]([/teal]value[teal]) })[/teal]

[teal];([/teal][b]function[/b][teal]() {[/teal] console[teal].[/teal][COLOR=orange]log[/color][teal]([/teal][i][green]'whatever'[/green][/i][teal]) })()[/teal]
( The strange placing of semicolons in such case is just a frequent habit. )


Feherke.
feherke.ga
 
Hi

Or in this extremely simple case, no ternary ;-) :
JavaScript:
lights [teal]=[/teal] income [teal]>=[/teal] [purple]500[/purple]

To stick with the original example :
Code:
[b]var[/b] bill [teal]=[/teal] [purple]500[/purple]
[b]var[/b] income [teal]=[/teal] [highlight #9f9][purple]800[/purple][/highlight]

[teal];([/teal]light [teal]=[/teal] income [teal]>=[/teal] bill[teal]) && ([/teal]income [teal]-=[/teal] bill[teal])[/teal]

console[teal].[/teal][COLOR=orange]log[/color][teal]([/teal][i][green]'light'[/green][/i][teal],[/teal] light[teal])[/teal]     [gray]// light true[/gray]
console[teal].[/teal][COLOR=orange]log[/color][teal]([/teal][i][green]'income'[/green][/i][teal],[/teal] income[teal])[/teal]   [gray]// income 300[/gray]
Code:
[b]var[/b] bill [teal]=[/teal] [purple]500[/purple]
[b]var[/b] income [teal]=[/teal] [highlight #f99][purple]400[/purple][/highlight]

[teal];([/teal]light [teal]=[/teal] income [teal]>=[/teal] bill[teal]) && ([/teal]income [teal]-=[/teal] bill[teal])[/teal]

console[teal].[/teal][COLOR=orange]log[/color][teal]([/teal][i][green]'light'[/green][/i][teal],[/teal] light[teal])[/teal]     [gray]// light false[/gray]
console[teal].[/teal][COLOR=orange]log[/color][teal]([/teal][i][green]'income'[/green][/i][teal],[/teal] income[teal])[/teal]   [gray]// income 400[/gray]

Feherke.
feherke.ga
 
Feherke said:
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top