In JavaScript, variables are used for storing and manipulating data. Variables allow the programmer to store values in the computer’s memory. Storing variables enables us to retrieve these values when we need them.
Variables
To declare a variable in JavaScript, we use the “var” “let” or “const” keyword, followed by the variable name.
let city;
In the above example, we declare a variable named “city” using the ‘let” keyword. Variables can hold various types of data, and their values can be changed throughout the program when using the “let” keyword. “var” and “const” cannot be reassigned, but that is another topic of its own.
Data Types
JavaScript lets us use following data types: numbers, strings, booleans, arrays, and objects. Below are examples of these JavaScript data types.
Numbers
JavaScript uses the number data type to represent numeric values.
let number = 10;
let price = 1.77;
The second example is a floating point number. The difference is that 1.77 a decimal and not an integer.
Strings
Strings are a set of characters enclosed in single or double quotes. Strings are really just text for the purpose of this blog.
let name = “Dot”;
let message = "Woof";
Boolean
Booleans, derived from Boolean Algebra, represent logical values and can be either true or false. Booleans are used for comparisons.
let dotIsADog = true;
let dotIsACat = false;
Arrays
Arrays are ordered collections of values, or a set of data enclosed in square brackets. They can hold elements of any data type, including numbers, strings, and even other arrays.
let numbers = [1, 2, 3, 4, 5];
let cars= [“Jeep”, “Cadillac”, “Chevrolet”];
Objects
Objects are data types that store key-value pairs.
let dog= {
name: “Dot”,
age: 3,
city: “Boston”'
};
Understanding variables and data types is critical in order to manipulate data in JavaScript.
Top comments (0)