EP25 – JavaScript Data Types

preview_player
Показать описание
Check out more great lessons via this link:

Although the JavaScript language doesn’t allow you to explicitly define a type when you declare a variable, this shouldn’t lead you to believe that JavaScript doesn’t have types (because it does).
JavaScript uses dynamic typing, and therefore this means that the JavaScript engine will choose the best data type for your variable based on its value.
So this begs the question, what data types could the JavaScript engine choose for my variable?
Here’s a quick summary of the JavaScript Data Types:

Data Type
Example

Boolean
 
var x = true;
var y = false;
 

Null
 
var person = null;
 

Undefined
 
var person = undefined;
var variableWithNothingAssigned;
 

Number
 
var x = 1.2;
var y = 32;
 

String
 
var aString = "What up dude!";
 

Symbol
 
const MY_KEY = Symbol();
let obj = {};
obj[MY_KEY] = 123;

Object
 
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
 

Boolean Data Type
The Boolean data type in JavaScript is a primitive type.
It is a pretty straight forward data type as it only contains two valid values (true, false).
Booleans are used when testing conditions. Conditions are found inside of control structures like an if statement (which we’ll talk about in the next article), or a while loop.
You’re probably already familiar with the concept of Booleans, so let’s move onto the next data type.
Null
It’s perhaps a bit strange to have null exist as a data type, but nevertheless it does in JavaScript.
Another strange bug inside of JavaScript is that if you inspect the “typeof” null, you’ll actually receive Object as the type.
In any case, in an attempt to stop confusing you, just know that null is used to represent “nothing”. It’s typically used as a place holder for an object.
Undefined
Not to be confused with null, the undefined data type in JavaScript is used to tell the coder that a variable has been declared but hasn’t yet been assigned any value.
So when we write the code var aVariable;, the resulting data type for aVariable would be undefined as we haven’t yet assigned a value.
What you could do if you wanted aVariable to no longer be assigned as an undefined data type, but still didn’t want to assign anything truly meaningful, you could assign it the null type.
Number
What I personally enjoy about JavaScript is that it only has one data type to specify a number, and that’s just the Number data type.
When you think about the Number data type in JavaScript as it relates to the Java language, you can state that a Number in JavaScript can be an Integer, a Double, a Float, or a BigDecimal all at the same time. So you don’t need to worry about the crazy details about how JavaScript is going to handle your numbers!
One helpful function in JavaScript that relates to the Number data type is the isNaN() function.
This function is used to determine if a value is “Not-a-Number”. It’s a bit confusing as you’re working on things in the “negative”, so if you want to check if something is a number you’re looking for a boolean value of false when running the isNaN() function.
String
A String is likely the most commonly used data type across any modern programming language.
A String is a data type that represents any combination of characters, special character and/or numbers that are assigned to a variable. Typically, Strings are used to store English language sentences, names, or even numbers!
You can think of Strings as a sort of “catch-all” for all the data types. What I mean by this is that if the JavaScript engine can’t figure out what data type to assign your variable, it’s probably going to just assign the String data type to your variable.
Strings also have a wide variety of functions and properties that make them extremely versatile and useful in the JavaScript language.
We’ll be diving into details in later articles with respect to all the things that you can do with Strings.
Symbol
The Symbol data type i
Рекомендации по теме