DEV Community

Cover image for JavaScript Variables and Scope: A Complete Beginner’s Guide with Examples
WISDOMUDO
WISDOMUDO

Posted on Edited on

JavaScript Variables and Scope: A Complete Beginner’s Guide with Examples

Introduction

Starting with JavaScript? One of the first things to master is how variables function and where they’re accessible in your code. Variables store information that your program uses, and their scope determines where you can effectively use them.

In this article, we’ll break down two key concepts:

  1. Variables in JavaScript
  2. Scope in JavaScript

We'll also include simple examples to help you understand how things work in real code.

Variables in JavaScript

What Is a Variable?

A variable is simply a named placeholder used to store a piece of information in your code. You can think of it as a box that holds information, like a number, text, or even more complex data.

Example:

let name = "Wisdom";
πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung

Here, "name" is a variable that stores the string "Wisdom."

Key Concepts of Variables

  • Name: The unique name you give to a variable (like 'name' or 'age').
  • Value: The data stored in the variable (e.g., "Wisdom" or 30).
  • Type: The specific form of data stored in a variable, such as **characters, numbers, or logical values.
  • Assignment: The act of storing a value into a variable using β€˜=’.
  • Mutability: Variables can be changed later (unless declared with const).

Declaring Variables

JavaScript provides three keywords to declare variables:

var:

  • Function-scoped or globally scoped.
  • Can be redeclared and reassigned.

Example:

var x = 5;
var x = 20; // No error

πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung

let:

  • Block-scoped (within {}).
  • Can be reassigned but not redeclared in the same scope.

Example:

let y = 10;
y = 15; // OK
// let y = 20; // Error if in the same block

πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung

const:

  • Block-scoped.
  • Cannot be reassigned or redeclared.

Example:

const c = 30;
// c = 40; // Error: Assignment to constant variable



πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung

Rules for Naming Variables

  • Must start with a letter, _, or $.
  • Cannot start with a number.
  • Are case-sensitive (myName β‰  , myname).
  • Use descriptive names for better readability (e.g., userName, totalScore).

Scope in JavaScript

What Is Scope?

The term scope in JavaScript describes where in your program a variable can be used. JavaScript includes three types of scope:

Global Scope
If you define a variable outside of any function or code block, it’s in the global scope and can be accessed throughout your entire program.

let name = "I'm global";

function goGlobal() {
console.log(name); // Very much accessible here}

goGlobal();
console.log(name); // Still accessible here

πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung

Local (Function) Scope

Variables declared inside a function are only accessible within that function.

Example:

function greet() {
let message = "Hello Wisdom";
console.log(message); // Effective!
}

greet();
// console.log(message); // Error: message is not defined

πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung

Block Scope

Variables declared with let or const inside a block (like if, for, or {}) are limited to that block.

Example:

if (true) {
let block = "I'm inside the block";
console.log(block); // Effective!
}

πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung

Remark: var does not respect block scope. It's on
ly function-scoped.

Conclusion

Understanding how variables and scope work in JavaScript is essential to writing clean and bug-free code. With this knowledge, you'll be able to structure your programs more effectively and avoid unexpected behavior due to variable conflicts.

Happy coding!

You can reach out to me via LinkedIn

Top comments (4)

πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung
 
andriy_ovcharov_312ead391 profile image
Andrii Ovcharov •

Interesting. Thanks for sharing!

πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung
 
wisdomudo profile image
WISDOMUDO •

You're welcome, I'm glad you find it useful.

πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung
 
jonrandy profile image
Jon Randy πŸŽ–οΈ •

JavaScript includes three types of scope:

Four actually. You've missed Module scope

πŸ”₯ Jadwal & Hasil: wisdomudo/javascript variables and scope a complete beginners guide with examples i - Jadwal Siaran Langsung
 
wisdomudo profile image
WISDOMUDO •

Thanks for posting out!