Introduction to JavaScript: Lesson 2 - JavaScript Variables and Data Types
Welcome back to the JavaScript section of our course! In this lesson, we'll dive deeper into JavaScript by exploring variables and data types, which are fundamental concepts in programming.
Understanding Variables Variables are containers used to store and manage data in your JavaScript programs. They allow you to store information and use it throughout your code.
Declaring Variables
In JavaScript, you can declare variables using the let
keyword. Variables can store various types of data, such as numbers, strings, and more.
let playerName = "Alice";
let playerScore = 100;
Data Types in JavaScript JavaScript has several data types, including:
- Numbers: Integers and decimals (e.g.,
42
,3.14
). - Strings: Text enclosed in single or double quotes (e.g.,
"Hello, world!"
). - Booleans: Represents true or false values (e.g.,
true
,false
). - Arrays: Ordered collections of values (e.g.,
[1, 2, 3]
). - Objects: Key-value pairs that represent structured data (e.g.,
{ name: "John", age: 30 }
). - Null: Represents the absence of a value.
- Undefined: Represents a variable that has been declared but not assigned a value.
Concatenation and Template Literals
You can combine strings using concatenation (+
operator) or use template literals to interpolate variables into strings.
let firstName = "John";
let lastName = "Doe";
// Using concatenation
let fullName = firstName + " " + lastName;
// Using template literals
let fullNameTemplate = `${firstName} ${lastName}`;
Your Task: Creating a User Profile For this lesson's assignment, create a JavaScript program that prompts the user for their name, age, and favorite color. Store this information in variables and display a message that includes their profile details using template literals.
Conclusion Congratulations! You've completed the second lesson in the JavaScript section. You now understand the concepts of variables and data types in JavaScript, which are essential for storing and managing information in your programs. In the next lesson, we'll explore decision-making in JavaScript using conditional statements. Keep up the great work on your web development journey!