Câu hỏi phỏng vấn Typescript
Câu hỏi

What is the purpose of Nullish Coalescing operator?

Câu trả lời

The Nullish Coalescing operator (??) in JavaScript, which is also applicable in TypeScript due to TypeScript's superset nature of JavaScript, serves a specific purpose: it provides a way to handle default values for potentially null or undefined expressions more precisely than other logical operators like ||. The operator works by evaluating its left-hand operand: if the operand is neither null nor undefined, it returns that operand; otherwise, it returns the right-hand operand. This behavior is particularly useful for assigning default values in cases where falsy values (such as 0, false, or an empty string "") are considered valid and should not be replaced by a default value.

The Nullish Coalescing operator is especially valuable because it distinguishes between nullish values (null and undefined) and other falsy values. This distinction is crucial in programming scenarios where the presence of a falsy value like 0 or an empty string "" has a specific, meaningful interpretation different from null or undefined, which typically represent the absence of a value. For example, when setting default values for...

junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What is the fundamental difference between Optional Chaining ( ?. ) and Non-null assertion operator ( ! )
in TypeScript?

middle

What is the difference between type and interface in TypeScript?

entry

What is TypeScript and why would I use it in place of JavaScript?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào