Full Stack Bootcamp
  • COURSE INFORMATION
    • Full Stack Bootcamp
  • Self-Onboarding
    • Get Started
    • Your Bootcamp
    • Participation & Conduct Protocols
    • Tools
      • Google Classroom
      • Slack
      • Google Calendar
      • Zoom
      • GitHub
      • Visual Studio Code
    • Study Strategies
    • Complete your Self-Onboarding
  • Overview
    • Prepare for the Course
    • Your Course
  • Foundations
    • First small Project
    • Working with the Browser
    • The Importance of Documentation
    • AI - How to use it for coding and learning
    • JavaScript
      • Get started with Javascript
      • Variables and Data Types
      • Operators
      • Conditionals
      • Loops
      • Arrays
      • Objects
      • Functions
      • Scope
      • Callbacks
      • Event Handling
      • Error Handling
      • Debugging
      • DOM Manipulation
      • Dynamic Rendering
      • Asynchronous Coding
      • Async/Await
      • Further Resources
  • 👏Credits
Powered by GitBook
On this page
  1. Foundations
  2. JavaScript

Variables and Data Types

Variables are the backbone of any programming language. They act as storage (like a box) containers for data, which can be manipulated and retrieved everywhere throughout your code. Understanding how to properly declare and use variables is crucial for writing effective and efficient JavaScript code.

1. Variable declaration

In JavaScript, variables are declared using three keywords: var, let, and const:

  • var: This keyword is used to declare a variable globally or locally to an entire function regardless of block scope. However, var has largely been replaced by let and const due to issues with scoping and hoisting. Variables declared with var are hoisted to the top of their containing function or script, which can sometimes lead to unexpected behavior.

Example:

var name = "John";
console.log(name); // "John"

var age = 30;
if (true) {
  var age = 25; // same variable
  console.log(age); // 25
}
console.log(age); // 25
  • let: Introduced in ES6 (ECMAScript 2015), let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is generally the preferred way to declare variables that can be reassigned. Unlike var, let declarations are not hoisted to the top of their containing block.

Example:

let age = 30;
console.log(age); // 30

if (true) {
  let age = 25; // different variable
  console.log(age); // 25
}
console.log(age); // 30
  • const: Also introduced in ES6, const allows you to declare variables whose values are constant. They are read-only and cannot be reassigned. However, it's important to note that if the variable is an array, the contents of the array can still be modified.

Example:

const birthYear = 1993;
console.log(birthYear); // 1993

// birthYear = 1994; // This will cause an error

2. Data Types

JavaScript has several data types, which are broadly categorized into primitive and non-primitive types. Knowing these types helps you understand how the language handles the data and what operations can be performed on it.

  • Primitive Types:

    • String: Represents textual data. Strings are created by enclosing the text in single or double quotes. Strings in JavaScript are immutable, meaning that once a string is created, it cannot be changed. Any operation that appears to modify a string actually creates a new string.

Example:

let greeting = "Hello, World!";
console.log(greeting); // "Hello, World!"
  • Number: Represents both integer and floating-point numbers. Unlike many other programming languages, JavaScript does not differentiate between integers and floats. Numbers in JavaScript can be positive or negative, and can include decimal points.

Example:

let num = 42;
let pi = 3.14;

console.log(num); // 42
console.log(pi); // 3.14
  • Boolean: Represents logical entities and can have only two values: true or false. Booleans are often used in conditional testing and control flow.

Example:

let isStudent = true;
let hasGraduated = false;

console.log(isStudent); // true
console.log(hasGraduated); // false
  • Null: Represents an intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations. Null is often used to indicate that a variable should not have a value.

Example:

let emptyValue = null;
console.log(emptyValue); // null
  • Undefined: Represents a variable that has been declared but not yet assigned a value. It is also a primitive type and treated as falsy. Undefined is often used to indicate that a variable has not been initialized.

Example:

let unassigned;
console.log(unassigned); // undefined
  • Symbol: Introduced in ES6, symbols are unique and immutable identifiers. They are often used to add unique property keys to an object that won’t collide with keys that any other code might add to the object. Symbols are not frequently used in beginner-level code but can be powerful in certain advanced scenarios.

Example:

let uniqueId = Symbol('id');
console.log(uniqueId); // Symbol(id)

In future lessons, we will cover more complex types like objects and arrays, which allow you to store collections of data and represent more complex entities. For now, focus on mastering the primitive types and understanding how to declare and use variables in JavaScript.

Use Case:

Suppose you're building a simple application to keep track of a user's information. You might use variables to store details like the user's name, age, and status as a student.

Example:

let userName = "Alice";
let userAge = 25;
let isStudent = true;

console.log("Name: " + userName); // "Name: Alice"
console.log("Age: " + userAge); // "Age: 25"
console.log("Is Student: " + isStudent); // "Is Student: true"

In this example:

  • We declare variables userName, userAge, and isStudent to store the details of a user.

  • We use console.log to print these details to the console, which helps in verifying that our variables are storing the correct values.

This simple use case demonstrates how variables and different data types can be combined to store and display information. Understanding these fundamentals is essential as you move on to more complex topics in JavaScript.

By mastering variables and data types, you'll have a strong foundation to build more sophisticated programs and applications. Each data type serves a specific purpose, and knowing when and how to use them is key to effective programming. As we progress, you'll learn more about arrays, objects, and other advanced concepts that will further enhance your coding skills.


PreviousGet started with JavascriptNextOperators

Last updated 10 months ago

Help us improve the content

You can leave comments .

🤩
here