//choose the best for your solution
var myVariable = 22; //this can be a string or number. var is globally defined
let myVariable = 22; //this can be a string or number. let is block scoped
const myVariable = 22; //this can be a string or number. const is block scoped and can't be reassigned
// 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
//You can make a variable by using:
var variable-name = 'defenition of the variable';
// Or you can use
let variable-name = 'defenition of the variable';
// JS Variables
var varExample = "var"; // var is use for variable in js the problem with it that it has complicated scope
let letExample = "let"; // let is pretty much same as var the only thing which make its more varriable is its variable scope.
const constExample = "const"; // const is use for constant values and this variable scope is sane as let
////We can define variables in 3 ways {var,let,const}///////
////syntax- var variable_name=value;
///// let variable_name=value;
/////const variable_name=value;
var myfristvariable="neha jaiswal";
const mysecondvariable=80;
let mythirdvar=abc;
// let & var are both editable variables:
var cow = 'moo';
let pig = 'oink';
cow = 10;
pig = 10;
// const is a permanent variable; you cannot change it.
const uncuttableGrass = '//';
uncuttableGrass = '___';
// above throws an error
//Making variables with let:
let numberOfFriends = 1;
//Incrementing:
numberOfFriends += 3; //numberOfFriends is now 4
// Variables with const
const minimumAge = 21; //CANNOT REASSIGN!
//Booleans - true or false values
true;
false;
let isHappy = true;
//Naming Conventions
// Use upper camel-cased names:
let numberOfChickens = 6; //GOOD
// NOT THE JS WAY:
// let number_of_chickens = 6;
function x() {
y = 1; // Genera un ReferenceError in strict mode
var z = 2;
}
x();
console.log(y); // scrive "1" in console
console.log(z); // Genera un ReferenceError: z non è definita fuori dalla funzione x
// 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
//An array is a type of object intended to be only assigned numeric keys
var tmp = []; // or: var tmp = {}
tmp.one = 1;
JSON.stringify(tmp);
// array:
'[]'
// object:
'{"one":1}'
//variables are a way to easily store data in javascript
//variables are declared by the var keyword, example...
var x = 10;
//or
var dog_name = "daisy";
//which can also be written as
var dog_name = 'daisy';
//same thing with single quotes and double quotes
var myString = "string"; //var can be redefined
var myInt = 34; //var can be redefined
const myString2 = "string"; //const cannot be redefined
const myInt2 = 69; //const cannot be redefined
var a; // variable
var b = "init"; // string
var c = "Hi" + " " + "Joe"; // = "Hi Joe"
var d = 1 + 2 + "3"; // = "33"
var e = [2,3,5,8]; // array
var f = false; // boolean
var g = /()/; // RegEx
var h = function(){}; // function object
const PI = 3.14; // constant
var a = 1, b = 2, c = a + b; // one line
let z = 'zzz'; // block scope local variable
/*Variables declared with var, let and const are quite similar when declared outside a block.
They all have Global Scope:*/
var x = 2; // Global scope
let x = 2; // Global scope
const x = 2; // Global scope
var a = 1;
b = 2;
delete this.a; // Genera un TypeError in strict mode. Altrimenti fallisce senza generare messaggi.
delete this.b;
console.log(a, b); // Genera un ReferenceError.
// La proprietà 'b' è stata cancellata e non esiste più.
//let and var are both editable variables and can be changed later on in your program;
let dog = 'Woof';
//dog is equal to the string 'Woof';
dog = false;
//You can changed the value of dog now because it was defined with let and not const;
let cow = 'Moo';
//cow is equal to the string 'Moo';
cow = true;
//You can change the value of cow later on because it is not defined with const;
//const is used when declaring a variable that can't be changed later on -- const stands for constant;
const pig = 'oink';
//This assigns the string 'oink' to pig which can not be changed because it is defined with const;
pig = 'snort';
//Above throws an error
//Good Job you now know how to declare variables using JavaScript!!!
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!
var a;
console.log(a); // scrive in console "undefined" o "" a seconda del browser usato.
console.log('still going...'); // scrive in console "still going...".
var a; // variable
var b = "init"; // string
var c = "Hi" + " " + "Joe"; // = "Hi Joe"
var d = 1 + 2 + "3"; // = "33"
var e = [2,3,5,8]; // array
var f = false; // boolean
var g = /()/; // RegEx
var h = function(){}; // function object
const PI = 3.14; // constant
var a = 1, b = 2, c = a + b; // one line
let z = 'zzz'; // block scope local variable
var x = 0; // x è dichiarata dentro l'ambiente file, poi le è assegnato valore 0
console.log(typeof z); // undefined, poichè z ancora non esiste
function a() { // quando a è chiamata,
var y = 2; // y è dichiarata dentro l'ambiente della funzione a, e le è assegnato valore 2
console.log(x, y); // 0 2
function b() { // quando b è chiamata
x = 3; // assegna 3 all'esistente ambiente x, non crea una nuova variabile globale
y = 4; // assegna 4 all'esistente esterna y, non crea una nuova variabile globale
z = 5; // crea una nuova variabile globale z e le assegna valore 5.
} // (Throws a ReferenceError in strict mode.)
b(); // chiamare b crea z come variabile globale
console.log(x, y, z); // 3 4 5
}
a(); // chiamando a si richiama b
console.log(x, z); // 3 5
console.log(typeof y); // non definito, perchè y è locale alla funzione a