Variabel JS
let x;
x = 102;
Rick Astley
let x;
x = 102;
var x = 0;
function f() {
var x = y = 1; // x è dichiarata localmente. y invece no!
}
f();
console.log(x, y); // Genera un ReferenceError in strict mode (y non è definita). 0, 1 altrimenti.
// In modalità non-strict mode:
// x è la globale come si ci aspettava
// però, y è uscita fuori dalla funzione!
// here are the ways to define variables
var x = "hi"; // for global variables
let x = "hi"; // for block-scoped variables
const x = "hi"; // for block-scoped variables which can not be reassigned
var is a keyword to define the varible in js but as of es-6 we, use let and const keywords for the same
// Can be a number
var myVar = 21
//Can be a string
var myVar2 = "string"
// Used in a function
function printVar(parvar) {
print(parvar);
}
printVar(myVar2);
// prints "string"
printVar(myVar);
// prints 21
// 3 ways to create
var mrVariable = 'x'; // You can set it to to a string ( '' ) or no. ( 0 ). It
// is globally defined
let myVariaible2 = 'y'; // You can set it to string or no. let is block scoped
const myVariable = 'z'; // It is block scoped, can be a string or no. and can't
//be reassigned