Javascript Developer JSE 40 01 · Free Practice Question Easy
Question 3
Q325 - Control Flow
Review the following code:
- let a = 10;
- if (a > 100)
- a = 20;
- console.log(a)
What will be displayed in the console as a result of its execution?
-
A
- 20
-
B
- 10
-
C
- 100
-
D
Nothing
Reveal correct answer
Correct answer: B
Explanation
Topics: if greater than operator
Try it yourself:
- let a = 10;
- if (a > 100)
- a = 20;
- console.log(a) // 10
Explanation:
10 is not greater than 100 and the if block is not executed.
Therefore a stays 10
The if statement executes a block of code if a specified condition is true
https://www.w3schools.com/jsref/jsref_if.asp
The greater than operator returns true if the left operand is
greater than the right operand, and false otherwise.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than
(There is a similar question Q625.)
Q325 (Please refer to this number, if you want to write me about this question.)
A. In this scenario, the if condition is evaluating whether 'a' is greater than 100, which is not true since 'a' is initialized to 10. As a result, the code inside the if block, where 'a' would be reassigned to 20, is not executed. Therefore, the value of 'a' remains 10, and it will be displayed in the console.
B. The code initializes the variable 'a' with a value of 10. The if statement checks if 'a' is greater than 100, which is not the case. Therefore, the code inside the if block, where 'a' would be assigned a value of 20, is not executed. As a result, the value of 'a' remains 10, and it will be displayed in the console.
C. The code does not have any logic that would result in the value of 'a' being set to 100. The if statement checks if 'a' is greater than 100, which is false in this case. Therefore, the value of 'a' remains 10, and it will be displayed in the console.
D. The code provided does not have any scenario where 'a' would not be displayed in the console. The variable 'a' is initialized to 10, and since the if condition is not met, the value of 'a' remains unchanged. Therefore, the value of 'a' (which is 10) will be displayed in the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
