JavaScript Questions: What is the Best Way to Work with undefined?

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ม.ค. 2025

ความคิดเห็น • 12

  • @satishm9534
    @satishm9534 6 ปีที่แล้ว +1

    Very well explained!

  • @aleksandregorov3685
    @aleksandregorov3685 6 ปีที่แล้ว

    Nice! I liked the use of typeof operator to check whether a variable exists.
    There is one thing I am a little confused about. You said that only the system assigns 'undefined' value, but I've seen that when you are trying to grab an element from the DOM, that does not exist, javascript returns 'null'. Is that somehow different or I'm missing something? Thanks for the videos!

    • @AllThingsJavaScript
      @AllThingsJavaScript  6 ปีที่แล้ว +2

      Glad you pointed this out! You are correct and I don't like that it is done this way. When working with DOM elements perhaps a way to think about it is you have tried to assign an object (DOM Element) to the variable. The object didn't exist so you end up assigning Null. But I would prefer it be consistent.

  • @alibaghban2934
    @alibaghban2934 4 ปีที่แล้ว

    Thanks for the video Sir.
    I have got a question. In this below code, why I am getting an empty string in the console while I am expecting 100. Since empty string and undefined are falsy and they will evaluate to false(coerced before comparing)
    var a = "";
    if (a == undefined) {
    a = 100
    }
    console.log(a);
    I appreciate your answer

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว +1

      An important thing to remember is an empty string doesn't coerce to undefined, it coerces to false. So it is not equal to undefined. It is equal (==) to false. Change your if statement: if (!a) OR if (a == false)

  • @marcusviniciusmatiasdearru1681
    @marcusviniciusmatiasdearru1681 7 ปีที่แล้ว

    Pretty clear.
    Tks.

  • @DanielWeikert
    @DanielWeikert 6 ปีที่แล้ว

    Thanks. Is there a particular reason why javascript requires a to be in quotes "a" when checking if variable is in window and otherwise simply use a without quotes? The quotes seem kind of strange to me as a beginner.

    • @AllThingsJavaScript
      @AllThingsJavaScript  6 ปีที่แล้ว

      Daniel, can you give me some sample code? I'm not quite sure what you are referring to.

  • @androidetube
    @androidetube 2 ปีที่แล้ว

    "a" in window is returning false for me. Did something change in js. since the video? I am using visual studio and the live server extension.

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 ปีที่แล้ว +1

      Nothing has changed. This is pretty basic JavaScript. double check your code. Maybe post it here. Also, could you be using strict mode?

    • @androidetube
      @androidetube 2 ปีที่แล้ว +1

      @@AllThingsJavaScript thanks, the mistake was that I didn't put in the quotes around the variable ("a"). Silly mistake

  • @seenuvasanv
    @seenuvasanv 7 ปีที่แล้ว

    Clear