Skip to content

Latest commit

 

History

History
221 lines (179 loc) · 5.29 KB

File metadata and controls

221 lines (179 loc) · 5.29 KB

JavaScript Variables

1. Introduction to Variables

Definition

  • A variable is a named storage location in computer memory
  • Used to store and manage data during program execution
  • Acts as a container that holds a specific value which can be modified or referenced

Key Characteristics

  • Provides a way to label and store data
  • Enables dynamic data manipulation
  • Supports various data types and scopes

2. Variable Declaration Methods

1. var (Legacy Declaration)

var oldStyleVariable = "I'm an old school variable";

Characteristics

  • Function-scoped or globally-scoped
  • Can be re-declared and updated
  • Hoisted to the top of its scope
  • Less predictable behavior
  • Not Recommended in Modern JavaScript

Example of Hoisting

console.log(x); // Outputs: undefined (not an error)
var x = 5;

2. let (Modern Block-Scoped Declaration)

let modernVariable = "I'm a block-scoped variable";

Characteristics

  • Block-scoped (limited to {} block)
  • Can be updated but not re-declared in same scope
  • More predictable behavior
  • Prevents unintended global variable creation
  • Recommended for Mutable Variables

Scope Demonstration

{
    let blockVariable = "I exist only in this block";
}
// blockVariable is not accessible outside the block

3. const (Constant Declaration)

const unchangeableValue = "I cannot be reassigned";

Characteristics

  • Block-scoped
  • Cannot be reassigned after initial value
  • Prevents accidental variable reassignment
  • Recommended as Default Declaration

Nuanced Behavior with Objects and Arrays

const person = { name: "Pankaj" };
person.name = "Updated Name"; // Allowed
// person = {}; // This would cause an error

3. Naming Conventions and Rules

Valid Variable Names

  • Must start with a letter, underscore (_), or dollar sign ($)
  • Can contain letters, numbers, underscores, dollar signs
  • Case-sensitive (age and Age are different variables)
  • No spaces or special characters allowed

Recommended Practices

  • Use camelCase for variable names
  • Choose descriptive and meaningful names
  • Avoid single-letter names (except in specific contexts like loops)

Good Examples

let firstName = "Pankaj";
let totalStudentCount = 100;
let isLoggedIn = true;

Bad Examples

let 2name = "Invalid";        // Starts with number
let user-name = "Not Good";   // Contains hyphen
let class = "Reserved word";  // Reserved keyword

4. Data Types in Variables

Primitive Types

  1. String: Text data

    let greeting = "Hello, World!";
  2. Number: Numeric values

    let age = 30;
    let temperature = 98.6;
  3. Boolean: True/False values

    let isStudent = true;
  4. Undefined: Declared but not assigned

    let undefinedVar;
  5. Null: Intentional absence of any object value

    let emptyValue = null;
  6. Symbol: Unique identifier (ES6)

    let uniqueKey = Symbol('description');
  7. BigInt: For very large integers

    let bigNumber = 1234567890123456789012345678901234567890n;

Reference Types

  1. Object: Key-value collections

    let person = {
        name: "Pankaj",
        age: 30,
        skills: ["JavaScript", "React"]
    };
  2. Array: Ordered list of values

    let fruits = ["Apple", "Banana", "Cherry"];

5. Variable Scope

Global Scope

  • Variables declared outside any function
  • Accessible everywhere in the code

Function Scope

  • Variables declared inside a function
  • Accessible only within that function

Block Scope

  • Variables declared inside {} blocks
  • Limited accessibility

Scope Example

let globalVar = "Global";

function exampleScope() {
    let functionVar = "Function Scope";
    
    if (true) {
        let blockVar = "Block Scope";
        console.log(globalVar);     // Accessible
        console.log(functionVar);   // Accessible
        console.log(blockVar);      // Accessible
    }
    
    // console.log(blockVar);  // Would cause an error
}

6. Best Practices

Declaration Recommendations

  1. Use const by default
  2. Use let when value will change
  3. Avoid var in modern JavaScript
  4. Initialize variables when declaring
  5. Group related declarations together

Performance and Memory

  • Declare variables as close to usage as possible
  • Release references when no longer needed
  • Be mindful of variable lifetime

7. Type Conversion

Implicit Conversion

let result = "5" + 3;  // Becomes "53" (string concatenation)
let math = "5" - 3;    // Becomes 2 (numeric subtraction)

Explicit Conversion

let stringNum = "100";
let number = Number(stringNum);    // Converts to number
let string = String(number);       // Converts to string
let boolean = Boolean(stringNum);  // Converts to boolean

Conclusion

Understanding variables is fundamental to JavaScript programming. Practice and consistent use will make these concepts second nature.

Quick Reference

  • const: Unchanging values
  • let: Changing values
  • var: Avoid in modern code

Happy Coding! 🚀👨‍💻