mirror of
https://github.com/Stone-Red-Code/website.git
synced 2026-09-06 23:43:45 +02:00
content(docs): update JS variables for clarity
This commit is contained in:
committed by
Jean-Philippe Sirois
parent
8f28201f6a
commit
defc87a36e
@@ -33,7 +33,7 @@ console.log(myVar);
|
|||||||
|
|
||||||
## Dynamic Types
|
## Dynamic Types
|
||||||
|
|
||||||
Variables in Javascript do not have explicit types, as some other languages (eg: Java) do. This means that when declaring a variable, you only use `name = value`, and don't need to add a type as well. The type is automatically chosen based on the value.
|
Variables in Javascript do not have explicit types, as some other languages (eg: Java, C++) do. This means that when declaring a variable, you only use `name = value`, and don't need to add a type as well. The type is automatically chosen based on the value.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
let myString = "Hello";
|
let myString = "Hello";
|
||||||
@@ -76,10 +76,10 @@ myArray.push(5);
|
|||||||
console.log(myArray); // [1,2,3,4,5]
|
console.log(myArray); // [1,2,3,4,5]
|
||||||
|
|
||||||
const myObj = { foo: "bar" };
|
const myObj = { foo: "bar" };
|
||||||
console.log(myObj); // {foo: bar}
|
console.log(myObj); // {foo: "bar"}
|
||||||
|
|
||||||
myObj.foo = "re-assigned";
|
myObj.foo = "re-assigned";
|
||||||
console.log(myObj); // {foo: re-assigned}
|
console.log(myObj); // {foo: "re-assigned"}
|
||||||
```
|
```
|
||||||
|
|
||||||
This, however, will still not work:
|
This, however, will still not work:
|
||||||
@@ -87,7 +87,7 @@ This, however, will still not work:
|
|||||||
```js
|
```js
|
||||||
const myArray = [1, 2, 3];
|
const myArray = [1, 2, 3];
|
||||||
|
|
||||||
myArray = [1, 2, 3, 4, 5]; // Error: myArray has already been defined
|
myArray = [1, 2, 3, 4, 5]; // Error: Assignment to constant variable
|
||||||
```
|
```
|
||||||
|
|
||||||
## What about var?
|
## What about var?
|
||||||
@@ -118,12 +118,12 @@ This is fixed using `let`
|
|||||||
for (let i = 0; i < 5; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
console.log(i);
|
console.log(i);
|
||||||
}
|
}
|
||||||
console.log(i); // Error: i is undefined
|
console.log(i); // Error: i is not defined
|
||||||
|
|
||||||
if (true) {
|
if (true) {
|
||||||
let myVar = "test";
|
let myVar = "test";
|
||||||
}
|
}
|
||||||
console.log(myVar); // Error: myVar is undefined
|
console.log(myVar); // Error: myVar is not defined
|
||||||
```
|
```
|
||||||
|
|
||||||
However, if the above code was in a function, this would not be the case. In the case of a function, the variable will be scoped to the given function, and not accessible outside it. Eg:
|
However, if the above code was in a function, this would not be the case. In the case of a function, the variable will be scoped to the given function, and not accessible outside it. Eg:
|
||||||
@@ -132,7 +132,7 @@ However, if the above code was in a function, this would not be the case. In the
|
|||||||
function myFunction() {
|
function myFunction() {
|
||||||
var myVar = "test";
|
var myVar = "test";
|
||||||
}
|
}
|
||||||
console.log(myVar); // Error: test is undefined
|
console.log(myVar); // Error: test is not defined
|
||||||
```
|
```
|
||||||
|
|
||||||
This can cause confusing behaviour, as `var` has different scoping behaviour depending on if its in a function or not. This is solved by using `let` or `const`.
|
This can cause confusing behaviour, as `var` has different scoping behaviour depending on if its in a function or not. This is solved by using `let` or `const`.
|
||||||
|
|||||||
Reference in New Issue
Block a user