CRT-600 practice question 21 of 30

Refer to the expression below: Let x = (‘1’ + 2) == (6 * 2); How should this expression be modified to ensure that evaluates to false?

Full set: 223 questions
Answer options
A: Let x = (‘1’ + ‘ 2’) == ( 6 * 2); Correct answer
B: Let x = (‘1’ + 2) == ( 6 * 2);
C: Let x = (1 + 2) == ( ‘6’ / 2);
D: Let x = (1 + 2 ) == ( 6 / 2);
Correct Answer:
A. Let x = (‘1’ + ‘ 2’) == ( 6 * 2);
Explanation

The original expression (‘1’ + 2) == (6 2) evaluates to true. The left side (‘1’ + 2) performs string concatenation, resulting in the string '12'. The right side (6 2) performs multiplication, resulting in the number 12. The loose equality operator == coerces the string '12' to a number, making the comparison 12 == 12, which is true.
Option A, (‘1’ + ‘ 2’) == ( 6 2), correctly modifies the expression to evaluate to false. The left side now concatenates two strings, '1' and ' 2', resulting in the string '1 2'. The right side is still the number 12. The comparison '1 2' == 12 evaluates to false because the string '1 2' (with a space) cannot be coerced into the number 12.

Why Incorrect

B. Let x = (‘1’ + 2) == ( 6 2);
This is identical to the original expression and therefore evaluates to true, not false.
C. Let x = (1 + 2) == ( ‘6’ / 2);
The left side evaluates to the number 3. The right side coerces '6' to a number for division, also resulting in 3. The expression 3 == 3 is true.
D. Let x = (1 + 2 ) == ( 6 / 2);
Both sides perform arithmetic operations, resulting in the number 3. The expression 3 == 3 is true.

References

1. Salesforce Lightning Web Components Developer Guide: The guide specifies that LWC uses modern JavaScript (ECMAScript). The behavior of operators like + and == is governed by JavaScript standards.

Source: Lightning Web Components Developer Guide

"JavaScript" section.

2. ECMAScript® 2024 Language Specification (ECMA-262): The official standard for JavaScript defines the behavior of operators.

Section 13.10.3

The Abstract Equality Comparison Algorithm: This section details how == performs type conversion. When comparing a String and a Number

it attempts to convert the String to a Number. ToNumber("12") yields 12

but ToNumber("1 2") yields NaN (Not-a-Number)

and NaN == 12 is false.

Section 13.15.2

The Addition operator (+): This section specifies that if either operand is a string

string concatenation is performed.

3. Apex Developer Guide: While the syntax is JavaScript-like

Apex exhibits similar behavior with string concatenation.

Source: Apex Developer Guide

"Expressions

" "Expression Operators." The + operator is overloaded to perform string concatenation if one of the operands is a String. For example

'Contact' + c results in a string.

Scroll to Top