2. Array
• Come accedo agli var days = ['Mon',
'Tues', 'Wed',
elementi
'Thurs', 'Fri',
dell'array? 'Sat', 'Sun'];
alert(days[0]);
3. Array [2]
• Come assegno un var days = ['Mon',
'Tues', 'Wed',
valore agli
'Thurs', 'Fri',
elementi 'Sat', 'Sun'];
dell'array?
days[0] = 'Monday';
days[1] = 'Tuesday';
10. if & else
if ( condition ) {
// some action
happens here
} else {
// some another
action
}
11. Condizioni
• == Equal to
• != Not equal to
• === Strict equal to
• !== Strict not equal to
• > Greater than
• < Less than
• >= Greater than or equal
• <= Less than or equal
12. Facciamo scegliere il colore?
if (enteredColor == 'red') {
alert('Rosso');
} else {
alert('Giallo');
}
15. else If
if (prima condizione) {
//istruzioni
• Posso andare
all'infinito, ma
} avrebbe senso?
else if (seconda • Sarebbe comodo
condizione) {
//istruzioni
da gestire?
}
else {
//istruzioni
}
16. Switch
switch (favoriteColor) {
if (favoriteColor == 'blue') {
case 'blue':
message = 'Blue is a cool color.';
} else if (favoriteColor == 'red') { message = 'Blue is a cool
color.';
message = 'Red is a warm color.';
} else if (favoriteColor == 'green') break;
{ case 'red':
message = 'Green is the color of the message = 'Red is a warm
leaves.'; color.';
} else { break;
message = 'What kind of favorite case 'green':
color is that?';
message = 'Green is the color
}
of the leaves.';
break;
default:
message = 'What kind of
favorite color is that?';
}
17. Switch [2]
switch(<variabile da valutare>) {
case <valore 1>: Il tutto è
//istruzioni
break; //si ferma qui probabilmente più
case <valore 2>:
leggibile
//istruzioni
break; //si ferma qui
…
case <valore n>:
//istruzioni
break; //si ferma qui
default:
//istruzioni
}