Variables and Data Types in JavaScript
Hey there! Welcome to this tutorial on variables and data types in JavaScript. You're already familiar with output statements, so let's dive into the world of variables and data types.
What are Variables?
In programming, a variable is a container that stores a value. Think of it like a labeled box where you can store a value. You can then use the variable to perform operations, display its value, or pass it to a function.
What are Data Types?
Data types determine the type of value a variable can hold. In JavaScript, we have several data types, including numbers, strings, booleans, and more.
Let's Create Some Variables!
Open your index.js
file and get ready to code!
Step 1: Create a Number Variable
Declare a variable myAge
and assign it the value 25
. Yes, you guessed it - we're creating a variable to store a person's age!
let myAge = 25
Run the code by clicking the "Run" button. You won't see anything in the console yet, but that's okay!
Step 2: Create a String Variable
Declare a variable myName
and assign it the value "John Doe"
. We're creating a variable to store a person's name!
let myName = "John Doe"
Run the code again.
Step 3: Create a Boolean Variable
Declare a variable isAdmin
and assign it the value true
. We're creating a variable to store a boolean value!
let isAdmin = true
Run the code once more.
Output Time!
Let's use console.log() to display the values of our variables. Add the following lines of code:
console.log("My age is:", myAge)console.log("My name is:", myName)console.log("Am I an admin?", isAdmin)
Run the code one last time.
Check your console output:
My age is: 25My name is: John DoeAm I an admin? true
Woohoo! You've successfully created variables with different data types and displayed their values using console.log()!
Real-World Example
Imagine you're building a simple quiz app. You could use variables to store the user's name, score, and whether they've completed the quiz. You could then use these variables to display the user's progress and provide feedback.
What's Next?
In the next tutorial, we'll explore Template Literals, a powerful feature in JavaScript that lets you create strings with embedded expressions.
Keep coding, and I'll see you in the next tutorial!
Tests