codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

4 / 72 topics
1Introduction to Go (Golang)2Installation and Setup3Hello World Program4Variables and Constants5Data Types6Operators7Control Structures (if, else, switch)8Functions9Defer Statement10Panic and Recover
Tutorials/Go (Golang)/Variables and Constants
🐹Go (Golang)

Variables and Constants

Updated 2026-04-20
3 min read

Variables and Constants in JavaScript

Variables and constants are fundamental concepts in any programming language, including JavaScript. They allow you to store data that can be manipulated or referenced throughout your program. In this section, we will explore the basics of variables and constants in JavaScript, including their declaration, types, scope, and best practices.

Variables

Declaration

Variables in JavaScript are declared using the let keyword for block-scoped variables or the const keyword for constants. You can also initialize a variable at the time of declaration.

// Declare a variable without initialization
let age;

// Declare and initialize a variable
let name = "Alice";

// Declare a constant
const pi = 3.14159;

Types

JavaScript is a dynamically typed language, meaning that the type of a variable can change at runtime. Common types include:

  • number: integer or floating-point numbers
  • string: text strings
  • boolean: boolean values (true or false)
  • object: objects (including arrays and functions)
  • null: null value
  • undefined: undefined value
let count = 10;
let pi = 3.14159;
let isDone = true;
let message = "Hello, JavaScript!";

Multiple Declarations

You can declare multiple variables at once using a single statement.

// Using let
let height = 180, weight = 75.5;

// Using const
const length = 20, width = 30;

Default Values

If a variable is declared but not initialized, it will have a default value of undefined.

let uninitializedVar; // Value is undefined

Naming Conventions

JavaScript follows camelCase for variable names. Use descriptive names to make your code readable.

// Good practice
userAge = 30;
userName = "JohnDoe";

// Bad practice
uA = 30;
un = "JohnDoe";

Constants

Constants are similar to variables, but their values cannot be changed once they are declared. Use the const keyword for constants.

Declaration

Constants can be declared using the const keyword followed by the constant name and value.

// Declare a constant
const Pi = 3.14159;

// Multiple constants declaration
const MaxAge = 100, MinAge = 0;

Types

Constants can be of any type, including strings, numbers, booleans, and objects.

const Name = "JavaScript";
const Version = 1.18;
const IsSupported = true;
const UnicodeChar = 'A';

Best Practices

  • Use const for values that do not change: Constants are useful for defining configuration settings, error messages, and other fixed values.
  • Avoid using global variables: Global variables can lead to code that is hard to maintain and debug. Use local variables or pass data through function parameters instead.
  • Use descriptive names: Choose meaningful names for your variables and constants to improve code readability.

Scope

Variables and constants have different scopes depending on where they are declared:

Block Scope

Variables declared inside a block (e.g., within a function, loop, or if statement) using let or const are only accessible within that block.

function main() {
    let x = 10;
    if (true) {
        let y = 20;
        console.log(x + y); // Accessible
    }
    console.log(x); // Accessible
    // console.log(y); // Error: y is not defined
}

Function Scope

Variables declared with var inside a function are accessible throughout the entire function.

function main() {
    var globalVar = 100;
    if (true) {
        var localVar = 200;
        console.log(localVar); // Accessible
    }
    console.log(globalVar); // Accessible
    console.log(localVar); // Accessible
}

Global Scope

Variables declared outside any function using var are accessible throughout the entire script.

var globalVar = 100;

function main() {
    console.log(globalVar);
}

function anotherFunction() {
    console.log(globalVar);
}

Type Inference

JavaScript supports type inference, allowing you to declare variables without explicitly specifying their types. The engine will infer the type based on the assigned value.

let inferredInt = 42; // number
let inferredFloat = 3.14; // number
let inferredString = "Hello"; // string

However, this feature is limited to local variables and cannot be used for constants or global variables.

Conclusion

Variables and constants are essential components of any JavaScript program. Understanding how to declare, initialize, and use them effectively will help you write clean, efficient, and maintainable code. By following best practices such as using descriptive names and avoiding global variables, you can improve the readability and reliability of your applications.

In the next section, we will explore control structures in JavaScript, including loops and conditional statements, which allow you to execute different blocks of code based on certain conditions.


PreviousHello World ProgramNext Data Types

Recommended Gear

Hello World ProgramData Types