JavaScript: Variables

JavaScript: Variables

In JavaScript, a variable is a named storage location for data or let's just say it's a bucket of information. Variables are used to store values such as numbers, strings, arrays, and objects, and can be declared and initialized with a value using the keywords.

A variable in JavaScript is a named data storage place, or, to put it simply, a bucket of data. Using var, let, or const keywords, a variable can be declared and initialised with a value to hold things like numbers, strings, arrays, and objects.

For example:

var x = 10;
let y = "hello";
const z = [1, 2, 3];
const MY_OBJECT = { key: 'value' };

Here, x is a variable with the value of 10, y is a variable with the value of "hello", and z is a variable with the value of [1, 2, 3].

Variables are useful because they allow you to store and manipulate data in your code, and can be accessed and updated as needed. They are an essential part of programming and are used in many different contexts.

Declaration and assignment of variables

A variable is given a specific value by a variable assignment, whereas a variable declaration creates a new variable and describes the type of value it will retain.

The var, let, or const keywords can be used before the variable name to declare a variable in JavaScript.

Consider this:

var x;  // variable declaration using the var keyword
let y;  // variable declaration using the let keyword
const z;  // variable declaration using the const keyword

You can also declare and assign a value to a variable in a single statement:

var x = 10;  // declaration and assignment using the var keyword
let y = 20;  // declaration and assignment using the let keyword
const z = 30;  // declaration and assignment using the const keyword

A variable can be declared as either global or local to the function in which it is declared using the varkeyword.

A variable that is local to the block in which it is declared is one that has the let keyword attached to it.

A variable that is local to the block in which it is declared and cannot be transferred is declared using the const keyword.

The assignment operator = can be used to give a value to a variable.

For instance:

x = 10;  // variable assignment
y = 20;  // variable assignment
z = 30;  // variable assignment

Keep in mind that a runtime error will occur if you attempt to assign a value to a variable that has the const keyword in its declaration but this doesn't prevent mutation. A const variable's value must be specified at the time of declaration and cannot be altered afterwards.

The following statement is correctly executed since the properties of objects allocated to variables with the const keyword are not protected.

const MY_OBJECT = { key: 'value' };
MY_OBJECT.key = 'otherValue';  // output: otherValue

Also, the contents of an array are not protected, so the following statement is correctly executed without problems.

const MY_ARRAY = ['HTML','CSS'];
MY_ARRAY.push('JAVASCRIPT');
console.log(MY_ARRAY); // ['HTML', 'CSS', 'JAVASCRIPT'];

Additionally, local variables that share the same name as a global variable are possible. Within the function in which it is declared, the local variable here takes precedence over the global variable.

var x = 10;  // global variable

function myFunction() {
  var x = 20;  // local variable with the same name as the global variable
  console.log(x);  // the local variable x is used, not the global variable
}

console.log(x);  // the global variable x is used

It's generally a good practice to use local variables whenever possible to avoid conflicts with global variables and to make your code easier to understand and maintain.

Variables and their scopes

Depending on where it is defined, a variable in JavaScript may have a global scope, function scope, or module scope.

Global scope: A variable having global scope can be accessed from anywhere in your code and is defined outside of any function or module. For instance:

var x = 10;  // global variable

function myFunction() {
  console.log(x);  // x can be accessed within myFunction
}

console.log(x);  // x can be accessed outside of any function

Function scope: Function-scoped variables are defined inside of functions and are only accessible from within those functions. For instance

function myFunction() {
  var y = 20;  // function-scoped variable
  console.log(y);  // y can be accessed within myFunction
}

console.log(y);  // y is not defined outside of myFunction

Module scope: In JavaScript, a module is a section of code that runs independently of the global scope. If a variable is not explicitly exported using the export keyword, it can only be accessed from within that module.

  • A module scope example is shown below:
// module.js

export const x = 10;  // x is exported and can be accessed from other modules

export function myFunction() {
  console.log(x);  // x can be accessed within myFunction
}

const y = 20;  // y is not exported and is only accessible within module.js

The import keyword can be used to import a variable or function from a module. For instance:

import { x, myFunction } from './module';  // import x and myFunction from module.js

console.log(x);  // 10
myFunction();  // prints 10

In conclusion, variables are a crucial component of JavaScript programming, and mastering their declaration, assignment, and use is necessary for writing good code. Depending on where they are defined in JavaScript, variables might have a global scope, function scope, or module scope.

To prevent conflicts and make your code simpler to understand and maintain, it's critical to select the proper scope for your variables.

Additionally, you can manage the reassignment and modification of your variables by using the let and const keywords, respectively. You may build dynamic, adaptable programmes that can handle changing data and requirements by using variables wisely.

Did you find this article valuable?

Support Ayobami J Haastrup by becoming a sponsor. Any amount is appreciated!