Skip to main content

Command Palette

Search for a command to run...

Introduction to Javascript Variables

Published
•2 min read•View as Markdown
S

I'm Samridhi Vashisht, a front-end developer skilled in React and Node.js. Aspiring full-stack developer with a passion for building user-friendly websites. Recently developed an interest in writing blogs to share my knowledge and experience.

Variables store data values that can be used and manipulated in a program.

Declaring Variables

To declare a variable in JavaScript, use the "var" keyword followed by the variable name. For example:

var x;

Initializing Variables

In addition to declaring a variable, you can also initialize it with a value when you create it. For example:

var x = 10;

Re-assigning Variables

The value stored in a variable can be changed during the execution of a program. To change the value of a variable, simply assign a new value to it using the assignment operator (=). For example

x = 20;

Data Types in JavaScript Variables

In JavaScript, variables can store values of different data types, including numbers, strings, Booleans, arrays, objects, and more.

  • Numbers: They can be integers (whole numbers) or floating-point numbers (numbers with decimal points). For example:
var num = 10;
var decimal = 10.5;
  • Strings: They are sequences of characters, and are denoted by either single quotes ('') or double quotes (""). For example:
var name = "John";
var message = 'Hello World!';
  • Booleans: They represent logical values and can be either true or false. For example:
var isTrue = true;
var isFalse = false;
  • Arrays: They are used to store collections of values, and are denoted by square brackets ([]). For example:
var numbers = [1, 2, 3, 4, 5];
var names = ["John", "Jane", "Jim"];
  • Objects: They are used to store collections of key-value pairs, and are denoted by curly braces ({}). For example:
var person = {
  name: "John",
  age: 30,
  location: "New York"
};

It's important to note that in JavaScript, variables are dynamically typed, meaning that the data type of a variable can change during the execution of a program.

var x = 10;
x = "Hello";

In the above example, the variable x is first declared and initialized with the value 10, which is a number. Later, the value of x is changed to the string "Hello".

Conclusion

Variables are an essential aspect of JavaScript programming, and they provide a way to store and manipulate data in your programs. Understanding how to work with variables is an essential step in becoming a proficient JavaScript programmer.

Thank you for taking the time to read this blog on JavaScript Variables. I hope the information provided was helpful and informative.