BTG Coding School

Basic Types in Javascript

Last time we learned how to write expressions, which ask Javascript to compute a value for us. We started just with numbers, since these work just like the numbers you’re familiar with in mathematics.

However, programming lets us do a lot more than just basic math. In order to move beyond numbers, we’ll need to introduce other types.

Types vary across programming languages, but in Javascript there are three main built-in types:

  • Numbers
  • Strings
  • Booleans

Numbers

We’ve worked with numbers in the previous lesson. In Javascript, numbers are stored as floating point numbers. Though you can read more into this if would like, for now it is enough to know that fractions are stored approximately. Rather than keeping track of the top and bottom numbers in a fraction, it is stored as an approximate value. Internally, this lets math be computed much more quickly. However, you may get some inexact results. In practice this doesn’t matter much, but it may surprise you if you need exact fractional numbers.

Strings

Strings are used to store text. We saw these briefly in the first lesson, where we used strings to store the message we wanted to display.

Some common operations on strings include:

What Formal name Javascript example
Combine concatenate
"Hi " + "Joe" 
Split split
 "hello world".split(' ')
Looking at a single character indexing
 "hello world"[0]

It is good to know the formal names in case you need to search online for help.

Booleans

Booleans are values that are either true or false. These are very helpful in making decisions (which we’ll see in detail later).

Boolean values can be created by comparing values. Some comparison operators you’ll want to know are:

english javascript example result
greater >
5 > 3
true
greater >
"cat" < "ant"
false
(compares alphabetically)
less than >
5 > 3
true
greater(or less) or equal >= or <=
5 >= 5
true
equal to ==
5 == 3
false
equal to ==
5 == "5"
true
equal, and the same type ===
"2" === 2
false

You might be surprised by the two different ways of comparing values. The == operator was the original one in the language, and came from its desire to make things easier on new programmers. This can have surprising results however. It converts one of the inputs to another type, then does the comparison. The conversion rules can be surprising though.

In practice, you’ll want to use === in most cases, and only == when you are expecting a conversion, such as dealing with strings entered by a user. I’ll be sure to point out such scenarios in the future.

Exercises

  1. Build a little program that will greet you. It should ask for your name (use the function ‘prompt’), then say “Hello” followed by your name. Its a good idea to build up your program in small pieces, so you can start with the prompt and make sure that part works.

  2. Build a little calculator to convert between inches and centimeters. If you dont know off hand, you can look up the formula. The input and output for this program is probably similar to the code you wrote above.

  3. Build a sort of password checker, by asking the user for the password. If the input equals “zyxxy”, say “correct”. Otherwise, say “incorrect”.

I’ve given you a function, called boolToMsg, that can convert true and false to these strings. You can use this when you write your program.

Answers

Note that you wouldn’t really write js code like this. We’ll learn some more features later that will let us write clearer code. But these can be built using only what you know so far:

Also note that in programming, theres usually more than one way to accomplish your goal. These represent one possible way to achieve your goal, but don’t worry if your solutions are slightly different but achieve the same result.

  1. alert("Hello " + prompt("What is your name?"))
    
  2. alert('Your length in centimeters: ' + (prompt('How many inches?') * 2.54))
    
  3. alert(boolToMsg(prompt('Whats the secret password?') == 'xyzzy'))
    

Next lesson: Variables

Up next we’ll look at “variables”, which can both help make our code cleaner and let us keep track of values.