Published on December 10, 2024By DeveloperBreeze

Section 2.1: Syntax and Basics

Tutorial 2.1.2: Variables and Constants


Variables and Constants in JavaScript

In JavaScript, variables and constants are used to store data that your program can use and manipulate. This tutorial explains how to declare, initialize, and use variables and constants effectively.


Declaring Variables

Variables in JavaScript can be declared using three keywords: var, let, and const.

  1. var (Legacy):
  • Used in older JavaScript versions but largely replaced by let and const.
  • Example:

     var message = "Hello, World!";
     console.log(message);
  1. let (Modern):
  • Preferred for declaring variables that can change value.
  • Example:

     let age = 25;
     age = 26; // Allowed
     console.log(age); // 26
  1. const (Constants):
  • Used for variables whose value should not change.
  • Example:

     const PI = 3.14;
     console.log(PI); // 3.14

Rules for Naming Variables

  1. Must start with a letter, underscore (_), or dollar sign ($).
  • Valid: name, _age, $price
  • Invalid: 1name, @value
  1. Case-sensitive:
  • age and Age are different.
  1. Avoid JavaScript reserved keywords (e.g., let, const, var).

Variable Initialization

Variables can be declared without assigning a value. Uninitialized variables are undefined.

Example:


let greeting;
console.log(greeting); // undefined

Using `let` and `const` in Practice

  1. When to Use let:
  • Use let for variables whose value may change.
  • Example:

     let score = 10;
     score += 5; // score is now 15
  1. When to Use const:
  • Use const for constants or variables that should remain unchanged.
  • Example:

     const API_KEY = "12345";
     // API_KEY = "67890"; // Error: Assignment to constant variable

Scope of Variables

  1. Block Scope (let and const):
  • Variables are accessible only within the block they are declared in.
  • Example:

     {
       let x = 10;
       console.log(x); // 10
     }
     console.log(x); // Error: x is not defined
  1. Function Scope (var):
  • Variables are accessible within the entire function they are declared in.
  • Example:

     function test() {
       var y = 20;
       console.log(y); // 20
     }
     console.log(y); // Error: y is not defined

Best Practices

  1. Use const by default; switch to let if the value needs to change.
  2. Avoid using var in modern JavaScript.
  3. Use meaningful names for variables.

Exercise

  1. Declare a variable name using let and assign it your name. Log it to the console.
  2. Declare a constant BIRTH_YEAR with your birth year. Try to change its value and note the error.

Example:


let name = "Alice";
console.log(name);

const BIRTH_YEAR = 1990;
// BIRTH_YEAR = 2000; // Error: Assignment to constant variable

Comments

Please log in to leave a comment.

Continue Reading:

Generate the Fibonacci series

Published on January 26, 2024

php

JavaScript Promise Example

Published on January 26, 2024

php

Building a Real-Time Chat Application with WebSockets in Node.js

Published on August 03, 2024

javascriptcsshtml

JavaScript Code Snippet: Fetch and Display Data from an API

Published on August 04, 2024

javascriptjson

Building a Modern Web Application with React and Redux

Published on August 05, 2024

javascript

Advanced TypeScript: Type Inference and Advanced Types

Published on August 05, 2024

typescript

Building Progressive Web Apps (PWAs) with Modern APIs

Published on August 05, 2024

jsonbash

Automatically add Tailwind CSS and jQuery classes to any table

Published on August 03, 2024

javascriptcsshtml