iT邦幫忙

2023 iThome 鐵人賽

DAY 5
0
Modern Web

前端知識系列 第 5

What is the difference between null, undefined and undeclared in JavaScript?

  • 分享至 

  • xImage
  •  

JavaScript Fundamentals

  1. Question: Can you explain the difference between null and undefined in JavaScript?
    • Your Original Answer: null means the variable has been assigned but doesn't have any value, and undefined means the variable hasn't been assigned any value.
    • Feedback: Your answer captures the essence but could be more precise.
    • Correct Answer: null is an intentional absence of any value or object, while undefined indicates that a variable has been declared but not yet initialized.

  1. Question: What are the implications of using null vs undefined when declaring a variable?
    • Your Original Answer: Repeated the same as question 1.
    • Feedback: The question aimed to explore the implications of using null vs undefined in different scenarios, which wasn't addressed.
    • Correct Answer: Using null indicates that the variable should have no value or object. Using undefined suggests that the variable will be assigned a value later.

  1. Question: How does JavaScript handle undeclared variables?
    • Your Original Answer: Using let or const will return a ReferenceError because they are in the Temporal Dead Zone (TDZ), and using var will return undefined.
    • Feedback: Your answer is accurate.
    • Correct Answer: Same as your original answer.

  1. Question: What are primitive data types in JavaScript?
    • Your Original Answer: Listed the seven primitive data types in JavaScript.
    • Feedback: You correctly identified the primitive types.
    • Correct Answer: The seven primitive data types in JavaScript are Number, String, Boolean, Null, Undefined, BigInt, and Symbol.

Practical Scenarios

  1. Question: Imagine you're fetching data from an API. How would you handle variables that are initially undefined but will be populated later?
    • Your Original Answer: You would use let data: Unknown | undefined = await fetch(url).
    • Feedback: The idea is correct, but the TypeScript type should be unknown (lowercase).
    • Correct Answer: let data: unknown | undefined = await fetch(url).

  1. Question: In the context of a User object, if a user doesn't have a profile picture, would you set the profilePicture property to null or undefined?
    • Your Original Answer: You would use null.
    • Feedback: Your answer is correct.
    • Correct Answer: Same as your original answer.

  1. Question: How would you define a TypeScript type for a user object that may or may not have a profile picture?
    • Your Original Answer: {image?: ProfilePic} or {image: ProfilePic | null}.
    • Feedback: Both approaches are valid.
    • Correct Answer: Same as your original answer.

Error Handling

  1. Question: What is a ReferenceError and when might you encounter one?
    • Your Original Answer: A ReferenceError occurs when variables declared with let or const are used before they are declared, due to the Temporal Dead Zone.
    • Feedback: Your answer is accurate.
    • Correct Answer: Same as your original answer.

  1. Question: How would you handle a situation where a variable might be undefined or null?
    • Your Original Answer: Use TypeScript to help me.
    • Feedback: Your answer is a bit vague. TypeScript can indeed help, but how would you specifically handle this situation?
    • Correct Answer: In TypeScript, you can use optional types or nullish coalescing to handle variables that might be undefined or null. For example, variable?.property or variable ?? 'default'.

Best Practices

  1. Question: When should you explicitly set a variable to null as opposed to letting it be undefined?

    • Your Original Answer: When the thing is truly existent, but you want to indicate that it doesn't have any value, you should use null.
    • Feedback: Your answer is correct and aligns well with best practices.
    • Correct Answer: You should set a variable to null when you want to explicitly indicate that the variable should have no value or object, as opposed to undefined, which suggests that the variable will be assigned a value later.
  2. Question: What are the pros and cons of using null and undefined interchangeably?

    • Your Original Answer: Pros, in TypeScript it is convenient to use optional types. Cons, it may not represent the correct meaning and could make the user confused.
    • Feedback: Your answer captures the key points but could be more detailed.
    • Correct Answer: Using null and undefined interchangeably can be convenient in some cases, especially when using optional types in TypeScript. However, it can also lead to confusion and bugs, as they are not strictly the same and can behave differently in certain contexts.

上一篇
JavaScript Equality Interview Preparation
下一篇
如何修復無法使用的 NX Console
系列文
前端知識30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言