JavaScript Data Types are NOT what you think they are (2024)

Sdílet
Vložit
  • čas přidán 30. 04. 2023
  • JavaScript Data Types
    let's talk about the different types that we have in JavaScript and luckily for us there aren't that many there's only seven of them we have numbers like five we have Booleans like true false we have strings like to be or not to be we have undefined which we've talked about we also have something called null we have another special one that came with es6 which is symbol and in here for now let's just say just me and then finally we have objects one two three four five six seven and you're looking at the list and thinking uh Andre I think you missed a bunch of others where are arrays or functions well before I get into that luckily for us JavaScript has an operator called type of that tells us the type of an item so let me comment this out and find out what the type of 5 is it's a number all right that makes sense What about type of true? If I run this, it's Boolean all right, so far, so good. nice and easy What if I type "to be or not to be"? That's a string, easy peasy. What about the type of undefined? Well, that's undefined; it's a special value in JavaScript. What about the type of no?
    Whoa, what just happened here? Null is an object. This is something that we're going to talk about a little bit more when we get into the object-oriented programming part of the course, but this is actually a mistake.
    I'm not making this up it's true even the creator of the language Brendan Ike who created JavaScript acknowledged it remember no programming language is perfect we all write programs that well can never be bug free and this is one of those cases that this should be null right it's an actual type in JavaScript it's a what we call A Primitive type but when we run the type of null operator we get object there was actually a proposal to fix this but because there's so much Legacy code that depends on this type of null being object that well they couldn't really change it because it will break a lot of programs so for now we're stuck with that and trust me there's a lot more weird things like that in JavaScript so let's skip over to the next one let's look at type of symbol if I hit run it's a symbol now a symbol like I said Is mu in es6 and it creates something unique for us that is a symbol value in this case just me is useful for identifying an object so symbols are usually used for object properties so that the object's property is unique we're not going to concern ourselves too much with this you can read up on it if you want but it's just the new type that we have that allow us to do some interesting things with object properties all right Last one type of actually before we get to type of let's go back here undefined and no what are the differences between the two well undefined is the absence of a definition so it's used as the default value when the JavaScript engine initializes our variables right remember our talk about hoisting well we use undefined for anything like that or even cases where let's say functions return undefined when they don't return anything there's no return keyword in a function or there's missing properties of an object undefined simply means well there's a variable there but there's nothing there no on the other hand is the absence of value it means there's no value there I know the distinction is a little bit hard to get but remember undefined is the absence of definition null is an absence of value there is no value there and this is something we'll get into a little bit later on in the course all right Where are we back to objects? If I do type of here and I hit run, it's an object. Nice, that makes sense. Okay, so where are the arrays? Let's have a look if I do type of array.
    THANKS FOR WATCHING AND DON'T FORGET TO LIKE, COMMENT, SUBSCRIBE, AND HIT THE BELL ICON TO WATCH THE LATEST VIDEO!
    #edudreams #js #javascript
    ~-~~-~~~-~~-~
    Please watch: "String manipulation in Python #codingtutorial #python #programminglanguage #coding"
    • Make Python Work For Y...
    ~-~~-~~~-~~-~

Komentáře •