Review the following code:
let x = 100; if (x < 100) x = 20; console.log(x)
What will be displayed in the console as a result of its execution?
- nothing
100
20
false
Explanation & Hint:
Here’s the breakdown of the code: let x = 100; – Declares a variable x and initializes it with the value 100.if (x < 100) – Checks if the value of x is less than 100. However, since x is equal to 100, the condition evaluates to false. x = 20; – Inside the if block, assigns the value 20 to x. However, since the condition in the if statement is false, this line of code will not be executed.
|
For more Questions and Answers:
JavaScipt Essentials 1 (JSE) | JSE1 – Module 4 Test Exam Answers Full 100%
Review the following code:
let x = 10; function test(x) { console.log(x); } test(20);
What will be displayed in the console as a result of its execution?
- Nothing will show up.
10
20
x
Explanation & Hint:
Let’s go through the code step by step:
When the test function is called with the argument 20, the local variable x within the function scope takes on the value of 20. Consequently, when console.log(x) is executed inside the function, it prints 20 to the console. |
For more Questions and Answers:
JavaScript Essentials 1 | JSE1 – Module 5 Test Exam Answers Full Score 100%
Review the following code:
let x = 10; let y = 20; function test(y) { console.log(y); } test(x);
What will be displayed in the console as a result of its execution?
- Nothing, because the function expects the
y
variable to be passed and receivesx
instead. 20
y
10
Explanation & Hint:
The code snippet will display the value 10 in the console. Let’s go through the code step by step:
When the test function is called with the argument x, the local variable y within the function scope takes on the value of the argument, which is the value of x. In this case, x is 10, so the local variable y becomes 10. Consequently, when console.log(y) is executed inside the function, it prints 10 to the console. Therefore, the result of executing the code will be 10 displayed in the console. |