Google Translate

English French German Spain Italian Dutch Russian Portuguese Japanese Korean Arabic Chinese Simplified by : BTF, ed. by JRD (me!)

mercoledì 18 agosto 2010

Using javascript's ternary operator with multiple statements or commands

Today I made a new discovery in javascript that does not seem to be very well documented. Maybe it's so obvious that it's given for granted. Well I'm self-taught so I can't take anything for granted. My discovery has to do with the use of the ternary operator, a kind of shortcut for conditional statements, with multiple statements. The ternary operator is a compact way of writing conditional statements, instead of writing out fully an "if...then...else" statement, that is "if (condition) { statements; } else { statements; }", you just write  "(condition) ? statement : statement;", which corresponds like this: "(if) ? then : else;". Now I was trying to use the ternary operatore but I needed to give multiple statements or commands in case of a postive result. And in javascript multiple statements should be separated by a semicolon, but a semicolon in the middle of a ternary operator will interrupt it. Well I found a solution: you just have to chain the statements or commands with a plus sign "+". Maybe some people already know that, I didn't. Here are some examples that illustrate concretely these usages.


Example of a conditional statement with a single command:

if(1==1) { alert("Hello") }
else { alert("Goodbye") }



Example of a conditional statement with multiple commands:

if(1==1) { alert("Hello"); alert("World!"); }
else { alert("Goodbye"); alert("Sky!"); }


Example of a ternary operator with a single command:

(1==1) ? alert("Hello") : alert("Goodbye");


Example of a ternary operator with multiple commands separated by a semicolon:

(1==1) ? alert("Hello"); alert("World!"); : alert("Goodbye"); alert("Sky!");

This form is not correct because the semicolon within the ternary operator will interrupt it.

Instead in this way it is possible to have multiple commands in the ternary operator:

Example of a ternary operator with multiple commands chained by a plus sign "+":

(1==1) ? alert("Hello")+alert("World!") : alert("Goodbye")+alert("Sky!");

I had no problems with this method, I've tried in a couple of circumstances and it's worked great.

Javascript: operatore ternario con molteplici istruzioni

Oggi ho fatto una nuova scoperta in javascript di cui non si trova facilmente documentazione. Si tratta dell'utilizzo dell'operatore ternario, una specie di scorciatioia per le istruzioni condizionali, con molteplici istruzioni. L'operatore ternario è un modo compatto per scrivere le istruzioni condizionali, in pratica anziché scrivere "if...then...else" ossia "if (condizione) { istruzioni; } else { istruzioni; }", si scrive "(condizione) ? istruzione : istruzione;", che corrisponde in questo modo: "(if) ? then : else;". Stavo cercando di sfruttare l'operatore ternario utilizzando però più istruzioni in caso di risultato positivo. Ora molteplici istruzioni in javascript si dovrebbero separare con un punto e virgola. Ma se metti un punto e virgola in mezzo all'operatore ternario si interrompe. Ebbene ho trovato una soluzione: basta concatenare le varie istruzioni con un "+", con un segno più. Spiego meglio con degli esempi concreti la mia soluzione.



Esempio di un condizionale con una sola istruzione:


if(1==1) { alert("Hello") }
else { alert("Goodbye") }



Esempio di un condizionale con molteplici istruzioni:

if(1==1) { alert("Hello"); alert("World!"); }
else { alert("Goodbye"); alert("Sky!"); }



Esempio di un'operatore ternario con una sola istruzione:

(1==1) ? alert("Hello") : alert("Goodbye");


Esempio di un'operatore ternario con molteplici istruzioni separate da punto e virgola:

(1==1) ? alert("Hello"); alert("World!"); : alert("Goodbye"); alert("Sky!");


Questa forma non è corretta perché il punto e virgola all'interno dell'operatore ternario lo interrompe.

Sarà possibile avere molteplici istruzioni? Ebbene sì ed ecco il modo:

Esempio di un'operatore ternario con molteplici istruzioni concatenate dal segno più:

(1==1) ? alert("Hello")+alert("World!") : alert("Goodbye")+alert("Sky!");

Non ho avuto alcun problema con questo metodo, ora l'ho provato in un paio di circostanze diverse e mi ha funzionato molto bene.