Skip to main content

Command Palette

Search for a command to run...

The Beginner’s Guide to JavaScript Variables, Data Types, and Conditional Logic

Published
•4 min read•View as Markdown
The Beginner’s Guide to JavaScript Variables, Data Types, and Conditional Logic
D

I’m an aspiring Software Developer with a strong interest in technology and problem-solving. My journey started as a Cyber Attendant, where I built hands-on skills in managing digital workflows and supporting everyday tech needs .Currently, I’m learning Python programming, Web Development (HTML, CSS, JavaScript).I believe in continuous growth, and I’m always eager to explore emerging technologies. My long-term goal is to leverage my skills in Software Engineering to design innovative, scalable, and impactful solutions for real-world problems since my passion lies in creating practical solutions.

JavaScript is one of the most popular programming languages in the world. Whether you are building websites, mobile apps, or even server-side applications, JavaScript is everywhere. As a beginner, it’s important to understand the foundational concepts that make up the language.

In this blog, we’ll cover:

  • Variables in JavaScript

  • Data Types in JavaScript

  • Scope of Variables

  • Conditional Statements (if, else if, else)

  • Switch Statement

By the end, you’ll have a solid foundation to start writing real-world logic in your JavaScript code.

1. Variables in JavaScript

A variable is a container that stores data. Think of it like a labeled box where you can keep information such as numbers, text, or objects.

In JavaScript, you can declare variables in three main ways:

var name = "Denzel";   // Old way (not commonly used today)
let age = 21;          // Modern way, value can change
const country = "Kenya"; // Constant, value cannot change
  • var → Function-scoped. Can be redeclared and updated (but often avoided because of unexpected behavior).

  • let → Block-scoped. Can be updated but not redeclared in the same scope.

  • const → Block-scoped. Cannot be updated or redeclared.

👉 Use let when you expect the value to change, and const when it should remain fixed.

2. Data Types in JavaScript

JavaScript has different data types. They fall into two categories: primitive and non-primitive.

Primitive Data Types

These are the most basic types of data.

let firstName = "Andlly"; // String
let age = 22;             // Number
let isStudent = true;     // Boolean
let nothing = null;       // Null (empty value)
let notAssigned;          // Undefined (no value yet)
let bigNum = 12345678901234567890n; // BigInt
let uniqueId = Symbol("id"); // Symbol
  • String → Text (inside quotes)

  • Number → All numbers (integers and decimals)

  • Boolean → True or false

  • Null → Empty or intentional "nothing"

  • Undefined → A variable declared but not given a value

  • BigInt → Very large numbers

  • Symbol → Unique identifiers

Non-Primitive Data Types

These are collections of values.

let colors = ["red", "green", "blue"]; // Array
let person = { name: "Denzel", age: 22 }; // Object

3. Scope of Variables

Scope refers to where a variable can be accessed in your code.

There are three main types:

  1. Global Scope → Available everywhere in the program.

  2. Function Scope → Variables declared inside a function can only be used within that function.

  3. Block Scope → Variables declared with let or const inside { } can only be used inside that block.

Example:

let globalVar = "I am global"; // Global scope

function example() {
  let functionVar = "I am inside a function"; // Function scope
  if (true) {
    let blockVar = "I am inside a block"; // Block scope
    console.log(blockVar); // ✅ Works
  }
  // console.log(blockVar); // ❌ Error (not accessible here)
}

example();
console.log(globalVar); // ✅ Works

4. Conditional Statements

Conditional statements allow your program to make decisions.

if Statement

Executes a block of code if the condition is true.

let age = 18;

if (age >= 18) {
  console.log("You are an adult.");
}

if...else Statement

Adds an alternative if the condition is false.

let age = 16;

if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are still a minor.");
}

if...else if...else Statement

Allows multiple conditions.

let marks = 75;

if (marks >= 80) {
  console.log("Grade: A");
} else if (marks >= 60) {
  console.log("Grade: B");
} else if (marks >= 40) {
  console.log("Grade: C");
} else {
  console.log("Grade: Fail");
}

5. Switch Statement

The switch statement is used when you want to check a variable against many possible values.

let day = 3;
let dayName;

switch (day) {
  case 1:
    dayName = "Monday";
    break;
  case 2:
    dayName = "Tuesday";
    break;
  case 3:
    dayName = "Wednesday";
    break;
  case 4:
    dayName = "Thursday";
    break;
  case 5:
    dayName = "Friday";
    break;
  default:
    dayName = "Weekend";
}

console.log("Today is " + dayName);

Always remember to use break; after each case, otherwise the code will continue running into the next case (this is called fall-through).

Final Thoughts

Understanding variables, data types, scopes, and conditional statements is the foundation of JavaScript. These concepts allow you to store data, control access, and make decisions in your programs.

Now that you know these basics, try practicing by:

  • Writing a program to check whether a number is even or odd.

  • Creating a simple grade calculator.

  • Building a program that prints the day of the week based on a number input.

The more you practice, the more comfortable you’ll become with JavaScript logic.