JavaScript / JavaScript Basics
Declaring Variables and Constants
In this tutorial, you'll explore how to declare variables and constants in JavaScript. You'll learn the differences between variables and constants, and when to use each of them.
Section overview
5 resourcesCovers the fundamental concepts of JavaScript, including variables, data types, and basic syntax.
Introduction
In this tutorial, we will be focusing on the basics of JavaScript - declaring variables and constants. Variables are fundamental to any programming language and they are used to store information. Constants, on the other hand, are similar to variables but, as the name implies, their value remains constant throughout the program.
By the end of this tutorial, you will learn:
- The difference between variables and constants
- How to declare and use variables and constants
- Good practices when working with variables and constants
Prerequisites: Basic understanding of JavaScript syntax and programming concepts.
Step-by-Step Guide
In JavaScript, you can declare a variable using var, let or const. var was the traditional way to declare variables but with ES6, let and const were introduced.
Variables
Variables declared with var are function-scoped, meaning they are only available within the function they're declared in. Variables declared with let are block-scoped, meaning they are only available within the block they're declared in.
To declare a variable, you would do the following:
var name = "John";
let age = 20;
Constants
Constants are block-scoped like let, but once a value is assigned to a const, it cannot be changed.
To declare a constant, you would do the following:
const PI = 3.14;
Code Examples
Example 1: Variables
// Declare a variable named 'greeting' and assign a string value to it
let greeting = "Hello, World!";
console.log(greeting); // Outputs: Hello, World!
Example 2: Constants
// Declare a constant named 'maxValue' and assign a number value to it
const maxValue = 100;
console.log(maxValue); // Outputs: 100
Summary
In this tutorial, we've covered the basics of declaring variables and constants in JavaScript. We learned that variables can be declared using var or let, and constants can be declared using const. We also saw the difference between function-scoped and block-scoped variables.
Next, you can dive deeper into JavaScript by learning about data types, control flow, and functions.
Practice Exercises
- Declare a variable named
cityand assign your favorite city's name to it, then print it to the console. - Declare a constant named
gravityand assign the value9.8to it, then print it to the console. - Try to assign a new value to the
gravityconstant and observe what happens.
Solutions
// Exercise 1
let city = "New York";
console.log(city); // Outputs: New York
// Exercise 2
const gravity = 9.8;
console.log(gravity); // Outputs: 9.8
// Exercise 3
gravity = 10; // This will throw an error because you can't reassign a value to a constant
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article