forked from pirple/Keeping-Up-With-the-Javascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Variables and Constants.js
executable file
·62 lines (47 loc) · 1.17 KB
/
Variables and Constants.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Variables and Constants
*
*/
// var, let, and const examples. Var and let can be reassigned to new values, but const cannot
var name = "Joe";
var myArray = [10, 20, 30];
var myObj = { a: 1, b: 2};
let anotherName = "Joelle";
let myOtherArray = [true, false, true, 100];
let myOtherObj = { a: 100, b: 200};
const yetAnotherName = "Joey";
const yetAnotherArr = ["yes", "no", "maybe"];
const yetAnotherObj = { id: 1212132, username: "es6dude"};
// Examples of var in a function - vars are function-scoped
var myName = "Chris";
function whatName() {
var myName = "Bob"
console.log(myName)
}
console.log(myName);
function sayHi() {
var shouldSayHi = true;
var reallySayHi = true;
if (shouldSayHi === true) {
let myName = "Chris";
}
console.log("Hi" + myName)
}
sayHi();
// Example of let in a function - let is block-scoped
function sayHiAgain() {
let shouldSayHi = false;
let myName;
if (shouldSayHi === true) {
myName = "Chris";
console.log("Hi" + " " + myName)
}
else {
myName = "Bob";
console.log("Hi" + " " + myName)
}
}
sayHiAgain();
// const example - const cannot be reassigned
const myArray = [1, 2, 3];
console.log(myArray;)