Q
How can you swap the values of two variables a and b in one line?

Answer & Solution

Answer: Option A
Solution:
Destructuring assignment allows swapping values with [a, b] = [b, a].
Related Questions on Average

Which assignment will fail?

A). const x = 10; x = 20;

B). let y = 5; y = 10;

C). var z = 15; z = 30;

D). let a = { b: 2 }; a.b = 3;

How to use destructuring to extract properties from an object?

A). { a, b } = { a: 1, b: 2 }

B). let { a, b } = { a: 1, b: 2 };

C). let a, b = { a: 1, b: 2 };

D). let (a, b) = { a: 1, b: 2 };

What is the outcome of let x = 8; x %= 3;?

A). 1

B). 2

C). 3

D). 5

What will be the result of let { a, b } = { a: 3, b: 4 };?

A). a is 4, b is 3

B). a is 3, b is 4

C). a is undefined, b is undefined

D). a is null, b is null

What will be the output of let { a, b } = { a: 1, b: 2, c: 3 };?

A). a is 1, b is 2

B). a is 2, b is 3

C). a is 1, b is undefined

D). a is 3, b is undefined

What does const do in JavaScript?

A). Declares a block-scoped variable

B). Declares a constant value

C). Declares a global variable

D). Declares a function

Can a constant objects properties be changed?

A). Yes

B). No

C). Only if redeclared

D). Only with special methods

Which of these correctly assigns values to variables using array destructuring?

A). [x, y, z] = [1, 2, 3]

B). [1, 2, 3] = [x, y, z]

C). let [x y z] = [1 2 3];

D). (x, y, z) = [1, 2, 3];

What does let { a: alpha, b: beta } = { a: 1, b: 2 }; do?

A). Creates variables alpha and beta with values 1 and 2

B). Creates variables a and b with values 1 and 2

C). Throws an error

D). Assigns default values

What does the assignment operator = do in JavaScript?

A). Compares two values

B). Assigns a value to a variable

C). Checks for equality

D). None of the above