Loading
Lesson 06
Courses / JavaScript for Complete Beginners
The typeof Operator

Summary

NBK Tech World - Understanding the typeof Operator in JavaScript

Welcome back to NBK Tech World! In this video, we will explore the typeof operator in JavaScript, a useful tool for understanding the different data types available in the language.

Overview of typeof

  • The typeof operator returns a string indicating the type of the unevaluated operand.
  • It's not commonly used in application development, but it enhances our understanding of JavaScript's data types.

Using typeof in JavaScript

Examples of Data Types:

  1. Strings

    • Example: let name = 'John';
    • typeof name returns "string".
  2. Numbers

    • Example: let num = 123;
    • typeof num returns "number".
  3. Strings vs. Numbers in Quotes

    • Example: let stringNum = '123';
    • typeof stringNum returns "string", illustrating the difference between a number and a string representation of a number.
  4. Arrays

    • Example: let arr = [];
    • typeof arr returns "object" (arrays are technically objects in JavaScript).
  5. Objects

    • Example: let obj = {};
    • typeof obj returns "object".
  6. Functions

    • Example: let logFunc = console.log;
    • typeof logFunc returns "function".
  7. Booleans

    • Example: let isTrue = true;
    • typeof isTrue returns "boolean".
  8. Undefined

    • Example: let undefinedVar;
    • typeof undefinedVar returns "undefined".

Additional Resources

  • For further reference, visit the Mozilla Developer Network (MDN) documentation on the typeof operator: MDN Documentation
  • MDN provides comprehensive documentation, examples, and information about browser compatibility.

Conclusion

Understanding the typeof operator is essential for new JavaScript developers as it helps clarify the nature of various data types within the language. If you have questions, don't hesitate to look up resources like the MDN documentation for guidance as you advance in your JavaScript journey.

Thank you for watching! If you found this video helpful, please like and subscribe for more content. Until next time, peace!

Video Transcript

Welcome back to NBK Tech World. In the last video, we learned some basic math in JavaScript. Now in this video, I would like to introduce you to an operator called TypeAuth. It's not that big of a deal, it's not something you're going to use a lot when you're out there developing your application, websites, and so on. But it's very useful to learn what's going on behind the scenes in JavaScript. So it's like I'm going to introduce it to you so that you can be curious about JavaScript, about the things or the objects, number of strings, whatever it is that you might already know, might expect or might not even know. So let me start off here. I want to, I want to switch to Chrome now because we did so far the next time. I want to do some, so I'm here in the browser console. Let's make this bigger for you. So the operator is called TypeAuth. Now this guy is going to give you whatever you type after it. You can check what the type is. For example, remember we defined strings, right? We have strings, say I have my name. This is a string because it has single quotes. What is the type of this guy? Well, it's going to tell you it's a string, okay? So we learned this guy is a string. Now let's try a number. One, two, three. What's the type of this guy? That's a number. Whoa, it's not a string. It's a number. So it's different, right? So wait a minute. What's the type of this guy? Just follow the basic principles. This guy is in quotes, so it's got to be a string. It's not a number. All right, so there's that difference. So be aware. A number inside quotes is not a number. It's actually a string. Now there are other things, other kinds of things in JavaScript that we haven't learned yet, but I can introduce to you now just to illustrate how the type of can help you learn JavaScript and help you define kinds of things, all right? So let's try an array. I'm going to put an empty array here for you. So an array is an object. Remember that. You probably won't, but it's a very important thing to know in JavaScript. But you don't have to know. Let's just illustrate type of, what about this guy? That's an object too. So these braces, whatever that is, we don't know it yet, right? We haven't learned, but it's called an object. Let's try something else. Remember we learned that function console log? What is the type of console.log? Wow, it's a function. Yeah. So it is indeed a function as we learned before. Now what else can we have? Ah, how about this? I can type this word true. What is the type of this word? Boolean. What the heck is a Boolean? So true and false are special keywords in the language of JavaScript. And it's called a Boolean value. It just means true or false, all right? Something is true, something is false. It's like a switch on and off. So think of that that way. So the word true doesn't have quotes. It's called a Boolean. So what's the opposite? I want you to try. Please try the opposite of true, which is false by the way. Type of false. It's also Boolean. Okay, so we have true and false and we learned about Boolean. Okay. Now, this little thing in JavaScript called undefined. I want to introduce undefined. It's also a special keyword in JavaScript, a very weird thing. And the type is undefined. It's weird. Yeah, but that's JavaScript for you. Anyway, we learned the type of operator to learn about the nature of things in JavaScript. We started off about the things we already knew, strings and numbers. And we learned the difference between, again, that a number is not a number under quotes. A number under quotes is a string, not a number. We learned about an empty array, an object. Actually, we're going to learn a lot more about these later, but I just wanted to illustrate that they're both an object. We learned, confirmed, right, that log is actually a function. Indeed, it is. We learned about new kinds of values in JavaScript, true and false. They're so-called Boolean. And then we learned about undefined, this weird thing in JavaScript, which actually is undefined. Now, what I want to leave through is reference. So you can start, get started with getting to know how to reference things in JavaScript, where to look for things, like you're doing your development and you forget certain things. That happens a lot, by the way. So I need some kind of guide or reference for me to look up things when I forget. So I want to give you a reference for all several types, for the types in JavaScript. So if you go and do a search, here I'm just going to search type of JavaScript. This way, you can get a very good documentation online. The one I want to direct to you to is the one from MDN, the Mozilla Developer Network, which came out first on this search engine, type of. So I'm going to choose this one. So it's the type of documentation from MDN, that's the developer.mozilla.org. It's pretty good documentation, in my opinion. And you can get some examples here. But what I want you to see is if you scroll down here at the description section, you can get some types, JavaScript types here. We learn number, there are numbers in JavaScript, there are strings. And today we learn this video about Poolean, the weird undefined, learn about the function type, the object type, and there are others here you can see. So if you forget, there's always this nice reference table you can look up. Give you mind MDN is a very good documentation, not only for this specific case, but for all other questions that you might have. As you get more advanced into JavaScript, you're going to be doing more complex stuff. You might forget a lot of things that happens a lot. And you have to look it up and review and remember them again. So very good reference with good examples here, console log type of whatever and gives you the output. Very nice. All right. So that's MDN. Also a very nice thing I forgot to mention is at the bottom here, you can see the browser compatibility because not everything is going to work on every single browser. So you have to make sure as you're out there in the real world developing applications that you have to cover. You might have to cover multiple browsers. So no one on our browser support the same thing. It used to be very predominant back in the day. It will be browsers did things differently. So you have to account for one browser and then figure out how to fix issues for another browser. But these days is pretty getting standard among different browsers. There's a standard and it's much less headache now, but be aware of browser compatibility or right. And IE specific notes. Oh, no. Anyway, thank you so much for watching. And please like and subscribe and until the next time. Peace.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: