Introduction to Variables
Variables are the backbone of any programming language, and JavaScript is no exception. They allow us to store and manipulate data in our code. Let's explore how to declare and use variables in JavaScript.
Declaring Variables
In JavaScript, we have three ways to declare variables:
var
(older method, function-scoped)let
(block-scoped, recommended for variables that can be reassigned)const
(block-scoped, for variables that won't be reassigned)
var oldSchool = "I'm function-scoped"; let modern = "I'm block-scoped"; const constant = "I can't be reassigned";
Pro tip: Always use let
or const
instead of var
for better code organization and to avoid potential issues with variable scoping.
Understanding Data Types
JavaScript has several built-in data types that you'll work with frequently. Let's break them down:
Primitive Data Types
-
Number: Represents both integer and floating-point numbers.
let age = 25; let pi = 3.14159;
-
String: Represents textual data.
let name = "Alice"; let greeting = 'Hello, World!';
-
Boolean: Represents true or false values.
let isLoggedIn = true; let hasPermission = false;
-
Undefined: Represents a variable that has been declared but not assigned a value.
let undefinedVar; console.log(undefinedVar); // Output: undefined
-
Null: Represents a deliberate non-value or absence of any object value.
let emptyValue = null;
-
Symbol: Represents a unique identifier (introduced in ES6).
const uniqueId = Symbol('id');
Complex Data Types
-
Object: Represents a collection of related data and/or functionality.
let person = { name: "Bob", age: 30, isStudent: false };
-
Array: A special type of object used to store multiple values in a single variable.
let fruits = ["apple", "banana", "orange"];
Basic Operators
Operators allow us to perform operations on variables and values. Let's explore some essential operators in JavaScript:
Arithmetic Operators
These operators perform mathematical operations:
let a = 10; let b = 5; console.log(a + b); // Addition: 15 console.log(a - b); // Subtraction: 5 console.log(a * b); // Multiplication: 50 console.log(a / b); // Division: 2 console.log(a % b); // Modulus (remainder): 0 console.log(a ** b); // Exponentiation: 100000
Assignment Operators
These operators assign values to variables:
let x = 5; x += 3; // Equivalent to: x = x + 3 console.log(x); // Output: 8 x -= 2; // Equivalent to: x = x - 2 console.log(x); // Output: 6 x *= 4; // Equivalent to: x = x * 4 console.log(x); // Output: 24 x /= 3; // Equivalent to: x = x / 3 console.log(x); // Output: 8
Comparison Operators
These operators compare two values and return a boolean:
let a = 5; let b = "5"; console.log(a == b); // Loose equality: true console.log(a === b); // Strict equality: false console.log(a != b); // Loose inequality: false console.log(a !== b); // Strict inequality: true console.log(a > 3); // Greater than: true console.log(a <= 5); // Less than or equal to: true
Logical Operators
These operators perform logical operations:
let isAdult = true; let hasLicense = false; console.log(isAdult && hasLicense); // Logical AND: false console.log(isAdult || hasLicense); // Logical OR: true console.log(!isAdult); // Logical NOT: false
Putting It All Together
Now that we've covered variables, data types, and basic operators, let's see how they work together in a practical example:
const username = "alice_wonder"; let age = 28; let isSubscribed = false; // Using template literals for string interpolation console.log(`Welcome, ${username}!`); if (age >= 18 && !isSubscribed) { console.log("You're eligible for a free trial subscription."); isSubscribed = true; } else if (age < 18) { console.log("Sorry, you must be 18 or older to subscribe."); } else { console.log("Thank you for being a subscriber!"); } // Using the ternary operator for concise conditionals let status = isSubscribed ? "Subscribed" : "Not subscribed"; console.log(`Subscription status: ${status}`);
This example demonstrates how variables, data types, and operators come together to create meaningful and interactive code. By understanding these fundamentals, you're well on your way to becoming proficient in JavaScript programming.