Variables

In JavaScript, we have several data types that are used to store different kinds of information:

  • Integer: Represents whole numbers without a decimal component. Example: let myNumber = 1000;
  • String: A sequence of characters enclosed within quotes. Example: let myText = "Hello!";
  • Float: Represents numbers that contain a decimal point. Example: let myFloat = 7.5;
  • Boolean: Represents one of two values, true or false. Examples: let myBool1 = true; let myBool2 = false;
  • Array: An ordered collection of items enclosed within square brackets. Example: let myList = [];
  • Object: A collection of key-value pairs enclosed within curly braces. Example: let myDictionary = {};

These data types are essential for storing and manipulating different kinds of information in JavaScript.

It's good practice to name your variables relevant to their purpose. For example, if I was creating a variable that would represent the cash price of an item, I might name it something like:

let itemCost = 1; // The cost of an item, which will be 1 dollar
let supplyRemaining = 2500000000; // The number of items in inventory that are remaining
let nameCTO = "John Woods";
let valueOfAQuarter = 0.25;

You might notice that I use camelCase for naming my variables. It's important to follow JavaScript naming conventions:

  • Variable names cannot start with a number.
  • Variable names cannot contain spaces.
  • Variable names can include letters, digits, underscores (_), and dollar signs ($), but should start with a letter, underscore, or dollar sign.

Here are some examples of incorrect variables that will not work in your code, and will cause an error, DO NOT USE:

let 3variable = 5;     // Starts with a number
let my-variable = 6;   // Contains a hyphen
let my variable = 7;   // Contains a space

General Arithmetic for Integers, Floats, and Strings

General arithmetic operations are a great place to start! Let's try creating a number variable, and logging it to the console.

If this is your first time programming, or even if you have programmed before, you'll quickly find out or are already familiar that logging is one of the most commonly used functions for debugging. It's how you "see" what the value is of your variable at a specific point in your code

let myNumber = 1;
let myNumberPlusOne = myNumber + 1;
console.log(myNumberPlusOne); // Output will be 2

There are several arithmetic operators available in JavaScript, and they are as follows:

let a = 10;
let b = 3;
let addition = a + b;          // Addition: 13
let subtraction = a - b;       // Subtraction: 7
let multiplication = a * b;    // Multiplication: 30
let division = a / b;          // Division: 3.3333333333333335
let floorDivision = Math.floor(a / b); // Floor Division: 3
let modulus = a % b;           // Modulus: 1
let exponentiation = a ** b;   // Exponentiation: 1000

For strings, '+' is used for concatenation:

let greeting = "Hello, " + "World!";
console.log(greeting); // Output: "Hello, World!"

Unlike Python, JavaScript does not support string multiplication using arithmetic operators; it results in NaN (Not a Number):

let string = "Hello";
let repeatedString = string * 3;  // This will result in NaN
console.log(repeatedString); // Output: NaN

Instead, use the repeat function for repeating strings:

let repeatedGreeting = string.repeat(3);
console.log(repeatedGreeting); // Output: "HelloHelloHello"

We won't dive into Arrays or Objects just yet, but it is important to note that arrays and objects are mutable, whereas strings are immutable in JavaScript.

When you hear mutable, think "can be changed", where mutable means it can be changed and immutable means it cannot be changed.

Example showing mutability of an array:

let myArray = [1, 2];
console.log(myArray[0]); // Output: 1
myArray[0] = 2;
console.log(myArray); // Output: [2, 2]

Example showing immutability (cannot be changed) of a string:

let myString = "Hello";
console.log(myString);  // Output: "Hello"
myString[0] = "J";  // This does not change the string
console.log(myString);  // Output: "Hello"

Quiz

Question 1

What data type is used to store whole numbers in JavaScript?





Question 2

Which of the following is a correct variable name in JavaScript?





Question 3

What will be the result of the following code?

let myNumber = 1;
let myNumberPlusOne = myNumber + 1;
console.log(myNumberPlusOne);




Question 4

Which arithmetic operator is used for exponentiation in JavaScript?





Question 5

What is the correct way to concatenate strings in JavaScript?





Code Editor