DEV Community

Dipankar Paul
Dipankar Paul

Posted on • Originally published at iamdipankarpaul.hashnode.dev on

Understanding JavaScript Variables: Declaration, Scope, and Data Types

Introduction:

Variables are a fundamental concept in programming, serving as storage locations in computer memory for data. JavaScript, a versatile and widely-used programming language, offers three different keywords for variable declaration: "var," "let," and "const." In this blog post, we'll explore the basics of JavaScript variables, including how to declare them, naming conventions, variable scope, data types, and how to assign values.

Declaration of Variables:

JavaScript provides three keywords for variable declaration:

  1. var : Used in earlier versions of JavaScript but not recommended for modern coding practices.
var myVar = "Hello world";
Enter fullscreen mode Exit fullscreen mode
  1. let : Preferred in modern JavaScript for variable declaration.
let myLet = 3;
Enter fullscreen mode Exit fullscreen mode
  1. const : Used to declare constants that cannot be reassigned.
const myConst = 3.14;
Enter fullscreen mode Exit fullscreen mode

Naming Convention:

When naming variables in JavaScript, it's essential to follow certain conventions:

  • Variable names should begin with a letter, dollar sign ($), or underscore (_).
  • Following the initial character, variable names can consist of letters, numbers, underscores, or dollar signs.
  • Variable names are case-sensitive in JavaScript.
// Valid variable names
let myVariable;
let _myVariable;
let $myVariable;
let my_variable;
let myVariable2;
Enter fullscreen mode Exit fullscreen mode

Scope of Variables:

The scope of a variable determines where it can be accessed in a program. JavaScript distinguishes between function-level and block-level scope:

  • Variables declared with "var" have function-level scope, meaning they can be accessed within the function in which they are declared.
// Example of function-level scope
function myFunction() {
 var x = 5;
}
console.log(x); // Throws an error because x is not defined outside the function
Enter fullscreen mode Exit fullscreen mode
  • Variables declared with "let" and "const" have block-level scope, meaning they can only be accessed within the block in which they are declared.
// Example of block-level scope
{
 let y = 10;
 const z = 15;
}
console.log(y); // Throws an error because y is not defined outside the block
console.log(z); // Throws an error because z is not defined outside the block
Enter fullscreen mode Exit fullscreen mode

Data Types of Variables:

JavaScript supports various data types that variables can hold:

  • Number : Represents numeric values.
  • String : Represents text values.
  • Boolean : Represents true/false values.
  • Null : Represents a deliberate non-value.
  • Undefined : Represents an uninitialized value.
  • Object : Represents complex entities.
  • Symbol : Represents unique identifiers.
// Examples of assigning different data types to variables
let myNumber = 42;
let myString = "Hello world";
let myBoolean = true;
let myNull = null;
let myUndefined = undefined;
let myObject = {
   firstName: "John",
   lastName: "Doe"
};
let mySymbol = Symbol("foo");
Enter fullscreen mode Exit fullscreen mode

Assignment of Variables:

Variables are assigned values using the assignment operator (=). The value stored in a variable can be changed or updated by assigning a new value to it.

// Assigning a value to a variable
let myVariable;
myVariable = "Hello world"; 
// Updating the value of a variable
myVariable = "Goodbye world";
Enter fullscreen mode Exit fullscreen mode

Key Takeaways:

  • Variables store data values and can be modified during program execution.
  • JavaScript offers "var," "let," and "const" for variable declaration, with "let" and "const" being preferred in modern coding.
  • Variable names should follow certain conventions.
  • Variable scope in JavaScript is either function-level or block-level.
  • JavaScript supports various data types for variables, including numbers, strings, booleans, objects, and symbols.
  • Variables can be assigned values using the assignment operator (=) and updated as needed.

Top comments (0)