My Code


#1

Someone could explain me the difference between let, var, and const?
I saw other coders that used var userName = ""
or in point 3 the hint suggests const userQuestion = 'blablabla'

//1
let userName = '';

//2
userName ? console.log(`Hello, ${userName}`) : console.log('Hello!');

//3

let userQuestion = 'Do I go in Amsterdam?';

//4
console.log(`The User asked: ${userQuestion}`);

//5
let randomNumber = Math.floor(Math.random() * 8);

//6
let eightBall = '';

switch (randomNumber) {
        case 0:
        eightBall = 'It is certain';
    break; 
    case 1:
        eightBall = 'It is decidedly so';
    break; 
    case 2:
        eightBall = 'Reply hazy try again';
    break;
    case 3:
        eightBall = 'Cannot predict now';
    break;
        case 4:
        eightBall = 'Do not count on it';
    break;
    case 5:
        eightBall = 'My sources say no';
    break;
    case 6:
        eightBall = 'Outlook not so good';
    break;
    case 7:
        eightBall = 'Signs point to yes';
    break;
}

//8
console.log(`The eight ball answered: ${eightBall}`);

#2

let and const where introduced in es6, they are “new”, before we only had var

the problem is that var has a functional scope (or global if there no enclosing function)

so let and const where introduced to give us block scope. For example:

if (true){
    var x = true; // global scope, no enclosing function
    let y = true; // block scope, only exist within if clause
}

console.log(x);
console.log(y); // will produce error

the difference between let and const? A constant variable can’t be changed/re-assigned:

const a = 'hello world'
a = 'error' // will produce error

#3

Thanks a lot, stetim94!