Skip to content

Numbers

Data Types

Unlike some other computer languages, JavaScript variables are officially referred to as being "untyped" or "loosely typed". This is because when a variable is set up, as mentioned above, it is simply a general purpose container, into which any of the major types of data may be placed. For JavaScript, these data types are shown below:

All data in a computer physically as it were are just binary digits, represented by zeros and ones. However, your programs will need to think of different pieces of data as different types of data. You can't add "Bob" (a string) to 1000 (a number) mathematically. All programming languages have a set of "data types". Here's JavaScript's:

  • Numbers
  • Strings
  • Booleans
  • Null
  • Undefined
  • Objects

We will focus on the first three for the first part of the semester. This is a very simple list--some languages have a much bigger list of possible data types. JavaScript is simpler in this area. Let's look at more details about the data types


Numbers

Some of the most important things we do with computers involve numbers, so variables will often hold references to numbers.

The number data type in JavaScript consists of, well, numbers.  Numbers are things with which we can do math.  We can count with them, have them be prices, add them together, divide them, etc.  Below are examples of numbers, in formats that are all valid for the JavaScript language:

4
1.50
-14
1000000
5e10
  • Numbers can be big or small, positive or negative, have decimals or not. In JavaScript they are all just numbers. There aren't different data types for different kinds of numbers.
  • How do we make a variable that's a number?
  • That's easy, just declare a variable and put a number in it. Just make sure you that you key in the number with no quotes around it (since that would create a string, even if it was all digits).
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    // Numbers in JavaScript

    let lineCount;
    let newPrice;
    let priceString;

    lineCount = 1;

    newPrice = 12.99;

    priceString = "12.99"; // This is not a number!
  • Do we have to tell JavaScript that a variable is a number? Nope.

Demo

Demo

  1. Number Variables
    • unit01/demos/demo-numbers.html

Converting strings to a number

  • When a user is asked to enter a number in a prompt dialog, what data type comes into your program?
  • Here's an example

1
2
3
4
5
    // What is peachPrice?

    let peachPrice;

    peachPrice = prompt("Enter a price of a peach.");
* It may surprise you, but the contents of the peachPrice variable is a string! Let's assume the user entered 4.99. That's just the characters "4", ".", "9", and "9" next to each other. It is not the number 4.99, yet. The most important thing about this is that you can't do any math with a string. * Anything that is entered into a computer by typing is a string. * But, what if you want to do some math? * Then you need to convert the string "4.99" into the number 4.99. * We will do this with the Number() function.

1
2
3
4
5
6
7
    // Converting an entered string to a number

    let peachPrice;

    peachPrice = prompt("Enter a price of a peach.");

    peachPrice = Number(peachPrice);
  • The Number() function takes the current value of the string in the variable, "4.99", and converts it to a number if it can. Then our line of code puts the new number back into the price variable.
  • Here's another look at this.

ConvertingStringsToNumbers


A Little Math

Note: When numbers are assigned to a variable with the assignment operator, they don't need to be converted to a number. They are already a number!  It is only when data enters the program via the prompt() method that the keyed in digits need to be converted to numeric format via the Number() function.

We will be doing a lot of basic arithmetic in our programs this semester. We'll start with the four simplest operations, adding, subtracting, multiplying, and dividing.

Addition (+)

We use the + (plus) symbol for addition.


Subtraction (-)

We use the - (dash) symbol for subtraction.


Multiplication (*)

We use the * (asterisk) symbol for multiplication.


Division (/)

We use the / (forward slash) symbol for division


Math with Variables

We can also perform math with variables. In fact, this is how we will usually do math very soon. (And then shortly after that, it will become an requirement.)


Hands on work

Demo

  1. Number Variables
    • unit01/demos/demo-numbers.html
  2. Number Math
    • unit01/demos/demo-math.html

Labs

  1. Lab05: Using Number Variables
    • unit01/labs/lab-05-numberVariables.html

Exercises

  1. Lab05 exercise
    • unit01/exercises/exercise-05.html