Skip to content

Latest commit

 

History

History
70 lines (48 loc) · 1.16 KB

File metadata and controls

70 lines (48 loc) · 1.16 KB

Code reading

Question 1

Take a look at the following code:

1    let x = 1;
2    function f1()
3    {
4        let x = 2;
5        console.log(x);
6    }
7    console.log(x);

Explain why line 4 and line 6 output different numbers.

The code above only logs 1, but if you call the function it logs 2. they have different outputs because of the scope, the x on line 4 exists only inside the f1 function.

Question 2

Take a look at the following code:

let x = 10

function f1()
{
    console.log(x)
    let y = 20
}

console.log(f1())
console.log(y)

What will be the output of this code. Explain your answer in 50 words or less.

It will log only x, you can't access something defined inside a function from the outside.

Question 3

Take a look at the following code:

const x = 9;

function f1(val) {
  val = val + 1;
  return val;
}

f1(x);
console.log(x);

const y = { x: 9 };

function f2(val) {
  val.x = val.x + 1;
  return val;
}

f2(y);
console.log(y);

What will be the output of this code. Explain your answer in 50 words or less.

the first const x is binded to number 9, the second const y is binded to an object and objects can be modified.