JS SWITCH

Standard

when there are a lot of choices that a program has to cover, instead of writing an else if statement each time, it’s possible to use the switch syntax

switch(expression){

case ‘ ‘:
// do things for this first case;
break;

case ‘ ‘:
// do things for this second case;
break;

default:
// do a default action if there is no matches;

switch allows you to preset a number of options (called cases), then check an expression to see if it matches any of them
if there is a match, the program will perform the action for the matching case
if there’s no match, a default option can be set and executed

var lunch = prompt(“what do you want for lunch?”,”sandwich, salad or pizza?”);

switch(lunch){

case ‘sandwich’:
console.log(“one sandwich, coming up”);
break;

case ‘salad’:
console.log(“sounds good, how about a caesar salad?”);
break;

case ‘pizza’:
console.log(“yummy, pizza”);
break;

case ‘pie’:
console.log(“pie’s not a meal”);
break;

default:
console.log(“huh, I’m not sure what ” + lunch + ” is”);
}

JS ISNAN

Standard

isNaN()
calling isNaN on something, checks if that thing is not a number

isNaN(“banana”);
// true

isNaN(NaN);
// true

isNaN(undefined);
// true

isNaN(42);
// false

but calling isNaN on a string that looks like a number, like “42”, javascript will try to help by automatically converting the string to the number 42 and return false (since 42 is a number)

this syntax can’t be used if the variable hasn’t been declared earlier
isNaN(dogs);

JS LOOPS

Standard

ciclo while con counter i
var loopThree = function(){
i = 0;
while(i < 3){
console.log(“I’m looping”);
i++;
}
};

loopThree();

ciclo while con validazione booleana
soloLoop()
var soloLoop = function(){
while(bool === true){
console.log(“looped once”);
bool = false
}
};

bool = true;
soloLoop();

ma il while loop si usa preferibilmente quando non si sa quante volte è necessario ripetere la stessa operazione.

la moneta viene lanciata finché non è uguale a 1
var coinFace = Math.floor(Math.random() * 2);

while(coinFace === 0){
console.log(“heads, flipping again”);
var coinFace = Math.floor(Math.random() * 2);
}
console.log(“tails, done flipping”);

differenza di sintassi tra while e for
var loop = function(){
// if(bool) is the abbreviation of if(bool===true)
while(bool){
console.log(“qualcosa”);
bool = false;
}
}
bool = true;
loop();

// for loop
for(i=0;i<1;i++){
console.log(“qualcosa”);
}

ciclo do, while
var loopCondition = false;

do {
console.log(“smetto di loopare perché la mia loop condition è ” + loopCondition);
} while (loopCondition);

all together
// while loop
var whileLoop = function(bool){
while(bool){
console.log(“bool is true”);
bool = false;
}
}

whileLoop(true);

// do, while loop
var doWhileLoop = function(bool){
do {
console.log(“I log even if bool is false”);
} while(bool);
}

doWhileLoop(false);

// for loop
var forLoop = function(){
for(i=0; i<1; i++){
console.log(“I log only one time”);
}
}

forLoop();

jsLoops

JS FIND NAME IN ARRAY

Standard

una stringa può essere trattata come un array di caratteri
var myArray = [“ciao”, “mondo”];
console.log(myArray[0]);
// “ciao”

la stessa logica funziona per le stringhe
var myName = “Giada”
console.log(myName[0]);
// “G”

il metodo push() aggiunge ciò che c’è nelle parentesi alla fine dell’array a cui viene applicato
myArray = [ ];
myArray.push(“ciao”);
console.log(myArray[0]);
// “ciao”

jsFindNameInArray

JS WINDOW.LOCATION

Standard

window.location object can be used to get the current page address (URL) and to redirect the browser to a new page

window.location.href
returns the href (URL) of the current page
returns the URL of the current page

document.getElementById(“demo”).innerHTML = “page location is ” + window.location.href;

// outputs
page location is https://www.w3schools.com/js/js_window_location.asp

window.location.hostname
returns the domain name of the web host
returns the name of the internet host (of the current page)

document.getElementById(“demo”).innerHTML = “page hostname is ” + window.location.hostname;

// outputs
page hostname is http://www.w3schools.com

window.location.pathname
returns the path and filename (the pathname) of the current page
document.getElementById(“demo”).innerHTML = “page path is ” + window.location.pathname;

// outputs
/js/js_window_location.asp

window.location.protocol
returns the web protocol used (http: or https:)
it is the web protocol of the page

document.getElementById(“demo”).innerHTML = “page protocol is ” + window.location.protocol;

// outputs
page protocol is https

window.location.port
returns the number of the internet host port (of the current page)

document.getElementById(“demo”).innerHTML = “port number is ” + window.location.port;

// outputs
port name is

** most browsers will not display default port numbers (80 for http and 443 for https) **

window.location.assign()
this method, window.location.assign(), loads a new document

<html>
<head>

function newDoc() {
window.location.assign(“https://www.w3schools.com&#8221;)
}

</head>
<body>

<input type=”button” value=”Load new document” onclick=”newDoc()”>

</body>
</html>

BOM OBJECTS

Standard

window object
l’oggetto window rappresenta una finestra aperta in un browser.
se un documento contiene iframe, definiti da appositi tag <iframe>, il browser crea un oggetto window per il documeno HTML, e un oggetto window addizionale per ogni frame.
l’oggetto window ha varie proprietà e metodi che sono supportati dalla maggior parte dei browser, ma non c’è uno standard ufficiale che li definisce.

lista delle window object properties
windowProperties

lista dei window object methods
windowMethods
navigator object
l’oggetto navigator contiene informazioni che riguardano il browser.
navigatorObject
screen object
l’oggetto screen contiene informazioni che riguardano lo schermo dell’utente.
screenObject
history object
l’oggetto history contiene le URL visitate dall’utente (all’interno della finestra del browser): l’oggetto history fa parte dell’oggetto window, si accede a esso tramite la window.history property.
historyObject
location object

l’oggetto location contiene informazioni che riguardano l’URL corrente: anch’esso è parte dell’oggetto window e ci si può accedere tramite la window.location property.
locationObject