Codeskill

Learn to code, step by step

JavaScript Basics: Understanding Variables, Types, and Declarations

We’ll look at variables, data types, and declarations in JavaScript. These are the building blocks. Everything else builds on top of them.

Variables

Variables store data. In JavaScript you declare them with var, let, or const. Each behaves slightly differently.

1. Var: the original way to declare variables. var is function-scoped – it is visible throughout the function where it is declared.

Example:

var greeting = "Hello, JavaScript World!";
console.log(greeting); // Outputs: Hello, JavaScript World!

2. Let: added in ES6 (ECMAScript 2015). let is block-scoped – the variable exists only inside the block (a loop, an if statement, and so on) where it is declared.

Example:

let age = 25;
if (age > 18) {
    let adult = true;
    console.log(adult); // Outputs: true
}
// console.log(adult); // Uncaught ReferenceError: adult is not defined

3. Const: also ES6. Use const for values that should not be reassigned.

Example:

const PI = 3.14;
// PI = 3.15; // TypeError: Assignment to constant variable.

Data types

JavaScript is dynamically typed – a variable is not locked to one type. The main types you will use:

1. String: text.

Example:

let name = "Alice";
console.log("Hello, " + name); // Outputs: Hello, Alice

2. Number: integers and floating-point values.

Example:

let distance = 150.5;
console.log(distance); // Outputs: 150.5

3. Boolean: true or false.

Example:

let isJavaScriptFun = true;
console.log(isJavaScriptFun); // Outputs: true

4. Undefined: a variable declared but not given a value.

Example:

let mood;
console.log(mood); // Outputs: undefined

5. Null: an intentional empty value.

Example:

let empty = null;
console.log(empty); // Outputs: null

Type coercion

JavaScript sometimes converts types automatically. Useful in places, confusing in others.

Example:

let num = "5" + 2; // "5" is coerced to a string
console.log(num); // Outputs: "52"

Arrays and objects

Arrays: ordered lists of values.

Example:

let colors = ["Red", "Green", "Blue"];
console.log(colors[0]); // Outputs: Red

Objects: collections of key-value pairs.

Example:

let person = {
    name: "Bob",
    age: 30
};
console.log(person.name); // Outputs: Bob

Operators

Operators let you work with variable values. Common ones:

  • Arithmetic operators: +, -, *, /
  • Assignment operators: =, +=, -=
  • Comparison operators: ==, ===, >, <
  • Logical operators: &&, ||, !

Example:

let x = 10;
let y = 5;
console.log(x * y); // Outputs: 50

Functions

Functions are reusable blocks of code that perform a task. You will use them constantly.

Example:

function greet(name) {
    return "Hello, " + name + "!";
}
console.log(greet("Alice")); // Outputs: Hello, Alice!

That covers variables, types, and declarations. Try writing a few snippets yourself before moving on to control structures.

PreviousGetting Started with JavaScript: Setting Up Your Development Environment