Variables & Data Types
Variables are fundamental building blocks in programming. They allow us to store, retrieve, and manipulate data in our programs. In this lesson, you'll learn what variables are, how to declare and initialize them, and the different data types available in most programming languages.
What are Variables?
A variable is a named storage location that contains a value. You can think of variables as labeled containers that hold data which can be changed throughout the program's execution.
Key Concepts:
- Variables have a name (identifier)
- Variables store data of specific types
- Variables can be reassigned with new values
- Variables must be declared before use
Here's how variables work in most programming languages:
// Declaring a variable
let age;
// Initializing a variable
age = 25;
// Declaring and initializing in one step
let name = "John";
// Changing a variable's value
age = 26;
Common Data Types
Different programming languages support various data types. Here are the most common ones:
Numeric Types
- Integer: Whole numbers without decimals
- Float/Double: Numbers with decimal points
let count = 42; // Integer
let price = 9.99; // Float
Text Types
- String: Text data enclosed in quotes
- Character: Single character (in some languages)
let message = "Hello, World!"; // String
let grade = 'A'; // Character
Boolean Type
- Boolean: Logical values (true/false)
let isActive = true;
let hasPermission = false;
Complex Types
- Array: Ordered collection of values
- Object: Collection of key-value pairs
let colors = ["red", "green", "blue"]; // Array
let person = { // Object
name: "John",
age: 30
};
Type Conversion
Sometimes you need to convert values from one data type to another. This is called type conversion or type casting.
// String to Number
let numStr = "42";
let num = Number(numStr); // Explicit conversion
let num2 = +numStr; // Shorthand conversion
// Number to String
let count = 100;
let countStr = String(count); // Explicit conversion
let countStr2 = count + ""; // Implicit conversion
// To Boolean
let active = Boolean(1); // true
let inactive = Boolean(0); // false
let emptyCheck = Boolean(""); // false
Common Pitfalls:
Be careful with automatic type conversion, especially in loosely typed languages. It can lead to unexpected results:
console.log("5" + 2); // Outputs: "52" (string concatenation)
console.log("5" - 2); // Outputs: 3 (numeric subtraction)
Variable Scope
Variable scope determines where in your code a variable can be accessed or modified.
Common Scope Types:
- Global scope: Variables accessible throughout the program
- Local/function scope: Variables accessible only within the function they're declared in
- Block scope: Variables accessible only within the block they're declared in (using let/const in JavaScript)
// Global variable
let globalVar = "I'm global";
function exampleFunction() {
// Local variable
let localVar = "I'm local";
// Block scope
if (true) {
let blockVar = "I'm in a block";
console.log(blockVar); // Accessible
}
console.log(localVar); // Accessible
console.log(globalVar); // Accessible
// console.log(blockVar); // Error: blockVar is not defined
}
exampleFunction();
console.log(globalVar); // Accessible
// console.log(localVar); // Error: localVar is not defined
Constants
Constants are like variables, but their values cannot be changed after initialization.
// Declaring a constant
const PI = 3.14159;
const BASE_URL = "https://api.example.com";
// This would cause an error
// PI = 3.14; // Error: Assignment to constant variable
Best Practice:
Use constants for values that should not change throughout your program. Common examples include configuration values, mathematical constants, and API endpoints.