Lesson 3
Exploring The Cosmos of JavaScript Data Types
Understanding Data Types in JavaScript

Hello, Space Explorer! Today, we're going to delve into JavaScript data types. Think of data types as different celestial objects in our universe, each with its unique properties and uses. We'll explore five core JavaScript data types — Number, String, Boolean, null, and undefined.

In JavaScript, data types are categorized based on their characteristics. Understanding this is like realizing the Earth is a planet, not a star or asteroid. This knowledge helps us predict behaviors in different situations.

Diving into the Numerical Data Type

First, we're going to discover numerical values through the Number data type, which stores numbers such as 10 (integer numbers) or -2.19 (floating point numbers, i.e., numbers with a decimal point). Let's visualize it:

JavaScript
1let numPlanets = 8; // Declaring an integer number variable 2console.log(numPlanets); // Prints: 8 3 4let planetWeight = 3.52; // Declaring a floating point number variable with a decimal point 5console.log(planetWeight); // Prints: 3.52

Upon running this code, you'll see 8 and 3.52 printed — the values of our declared numPlanets and planetWeight variables.

Exploring the String Data Type

Next, we'll traverse the textual space with String, which represents any text data. Let's create planetName and assign it "Earth".

JavaScript
1let planetName = "Earth"; // Declaring a string variable 2console.log(planetName); // Prints: "Earth"

Seeing "Earth" printed to the console illustrates how the String variable planetName works!

Discovering the Boolean Data Type

In JavaScript's universe, the Boolean data type is like binary stars, manifesting as either true (correct, valid) or false (not correct, not valid). They're often used in programming to make decisions. Let's see it in action:

JavaScript
1let earthIsAPlanet = true; // Declaring a boolean variable 2console.log(earthIsAPlanet); // Prints: true 3 4let appleIsAPlanet = false; // Declaring another boolean variable 5console.log(appleIsAPlanet); // Prints: false

When you run this code, true and false are printed, which indicates that earthIsAPlanet is true and appleIsAPlanet is false.

Null Data: Something or Nothing?

null in JavaScript represents a lack of value for a particular variable. Let's exemplify this:

JavaScript
1let spaceOutsideUniverse = null; // Declaring a variable with 'null' value 2console.log(spaceOutsideUniverse); // Prints: null

When you run this code, null is printed, showcasing spaceOutsideUniverse as having no value — null.

Undefined: What Does It Mean?

undefined, a designation for a variable that is declared but not assigned a value, is akin to spotting a planet about which we know nothing.

JavaScript
1let newPlanet; // Declaring a variable without assigning a value 2console.log(newPlanet); // Prints: undefined

When you run this code, undefined is printed, reflecting that newPlanet has been declared but not assigned.

Null vs Undefined: A Quick Comparison

In JavaScript, both null and undefined represent a lack of value, but their usage differs. The value null is assigned to a variable to intentionally indicate an absence of meaningful value. On the other hand, undefined is automatically assigned to a variable that has been declared but hasn't been given a value. Its presence generally implies that a value is yet to be assigned.

Here's a simple illustration:

JavaScript
1let intentionalLack = null; // variable intentionally given no value 2console.log(intentionalLack); // Prints: null 3 4let unintentionalLack; // variable declared but not assigned a value 5console.log(unintentionalLack); // Prints: undefined

The console outputs demonstrate that intentionalLack is purposely empty, while unintentionalLack is waiting to be assigned a value.

Getting a type of the variable

To get a type of variable, you can use the typeof operator. Here is an example:

JavaScript
1let integerNumber = 8; 2let floatNumber = 3.52; 3let stringVar = "Earth"; 4let boolVar = true; 5let nullVar = null; 6let undefinedVar = undefined; 7 8console.log(typeof integerNumber); # Prints: number 9console.log(typeof floatNumber); # Prints: number 10console.log(typeof stringVar); # Prints: string 11console.log(typeof boolVar); # Prints: boolean 12console.log(typeof nullVar); # Prints: object (as null can be anything) 13console.log(typeof undefinedVar); # Prints: undefined
Lesson Summary and the Next Steps

Great job! We've journeyed through JavaScript data types: Number, String, Boolean, null, and undefined. Now, let's progress toward practice exercises to solidify this knowledge. As you learn more with each exercise, you'll gain a deeper understanding of JavaScript data types. Remember, practice is key! Let's continue to explore!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.