Files
freeCodeCamp/curriculum/challenges/italian/02-javascript-algorithms-and-data-structures/basic-javascript/explore-differences-between-the-var-and-let-keywords.md
Kristofer Koishigawa bcc9beff1f feat(curriculum): introduce let and const earlier (#43133)
* fix: move "Explore Differences Between..." to basic JS, update seed and tests

* fix: resequence "Declare String Variables"

* fix: move "Declare a Read-Only Variable..." to basic JS, update seed and tests

* fix: revert changes to non-English "Explore Differences Between..." test text

* fix: revert test strings, solutions, and seeds for non-English challenges

* fix: update "Declare String Variables" description

* fix: sync quotation marks in description and seed

* fix: modify note in "Declare a Read-Only..." challenge

* fix: update operator and compound assignment challenges

* fix: update string challenges

* fix: update array and array method challenges

* fix: update function and scope challenges, resequence slightly

* fix: "Word Blanks" solution

* fix: add spacing to seed

* fix: concatenating += challenge spacing

* fix: appending variables to strings spacing

* fix: find the length of a string spacing

* fix: removed instances of removedFromMyArray = 0

* fix: switch challenges

* fix: function argument and param spacing

* fix: update counting cards, object challenges, and record collection

* fix: finish rest of Basic JS section

* fix: introducing else statements solution

* fix: update spacing and wording

* fix: update wording for const challenge

* fix: update functional programming challenges

* fix: intermediate algorithms and cert challenges

* fix: revert some spacing and remove comments for fp challenge solutions

* feat: add notes with links to moved let and const challenges in first two es6 challenges

* fix: update es6 intro text

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* fix: concatenating strings with plus operator seed

* fix: add comments back to Declare a Read-Only Variable... seed

* feat: add es6 to basic javascript redirect tests for let and const challenges

* fix: revert "Concatenating Strings with Plus Operator" seed

* fix: move test file to cypress/integration/learn/redirects, separate redirect tests

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
2021-10-25 17:55:58 +01:00

2.4 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7b87367417b2b2512b3f Esplorare le differenze tra le parole chiave var e let 1 301202 explore-differences-between-the-var-and-let-keywords

--description--

Uno dei maggiori problemi quando si dichiarano delle variabili con la parola chiave var è che è possibile sovrascrivere le dichiarazioni delle variabili senza errori.

var camper = 'James';
var camper = 'David';
console.log(camper);

Qui la console mostrerà la stringa David.

Come puoi vedere nel codice qui sopra, la variabile camper è originariamente dichiarata come James per poi essere sovrascritta con David. In una piccola applicazione si potrebbe non incorrere in questo tipo di problema, ma quando il codice diventa più grande, potresti accidentalmente sovrascrivere una variabile che non hai intenzione di sovrascrivere. Poiché questo comportamento non lancia un errore, la ricerca e la correzione di bug diventa più difficile.
Una nuova parola chiave, chiamata let, è stata introdotta in ES6 per risolvere questo potenziale problema con la parola chiave var. Se dovessi sostituire var con let nelle dichiarazioni delle variabili nel codice sopra, il risultato sarebbe un errore.

let camper = 'James';
let camper = 'David';

Questo errore può essere visto nella console del tuo browser. Quindi, a differenza di var, quando si utilizza let, una variabile con lo stesso nome può essere dichiarata solo una volta. Nota l'"use strict". Questo abilita la Strict Mode (Modalità Rigorosa), che cattura gli errori di codifica comuni e le azioni "non sicure". Per esempio:

"use strict";
x = 3.14;

Questo mostrerà l'errore x is not defined.

--instructions--

Aggiorna il codice in modo che utilizzi solo la parola chiave let.

--hints--

var non dovrebbe esistere nel codice.

(getUserInput) => assert(!getUserInput('index').match(/var/g));

catName dovrebbe essere uguale alla stringa Oliver.

assert(catName === 'Oliver');

quote dovrebbe essere uguale alla stringa Oliver says Meow!

assert(quote === 'Oliver says Meow!');

--seed--

--seed-contents--

var catName;
var quote;
function catTalk() {
  "use strict";

  catName = "Oliver";
  quote = catName + " says Meow!";

}
catTalk();

--solutions--

let catName;
let quote;
function catTalk() {
  'use strict';

  catName = 'Oliver';
  quote = catName + ' says Meow!';
}
catTalk();