Numbers Strings And Booleans
Use the browser's console
Understand the primitive types of numbers, strings and booleans
Using the Browser's Console
To open the console on a Mac, use the shortcut
J. To open the console in Windows or Linux, use the keyboard shortcut
J. Alternatively, right click, select 'Inspect Element' from the right-click menu, and click the 'Console' tab.
>prompt. The console will also show us the return value of an expression we type and will display any errors we get.
In the console, type
5and press enter. Notice that it will display the value
5in response. Thus, our return value for the expression
typeof(5)and note what kind of object
Try creating decimal numbers.
Try creating irrational numbers (a number that can only be fully expressed as the ratio of two numbers, like 2/3). Notice that it will convert it to a decimal.
Try adding or subtracting numbers in the console by typing
6 + 12or
15 - 32.
Try an edge case with numbers, like
12 / 0.
To assign a number to a variable, type
favoriteNumber = 5into the console prompt. Then use favoriteNumber in the next expression, like
More complex math, like exponents, will require us to use the Math object, as in
Math.pow(12, 2), but that shouldn't stop us from trying it out!
Bonus Points: Check out Mozilla Developer Network Docs for the Math object, and try using some of the other methods they describe in your console!
Strings are units of text, and we encapsulate them in
Try creating a string by typing
'this is a string'into the console prompt.
You can grab a string's individual characters with
'this is a string', where the number 6 is the index of the character you want, starting at 0.
Concatenate strings with
'my name is' + 'Michelle' + '.'.
Assign a string to a variable by writing
myName = 'Michelle'.
Use the variable as you would a literal string:
'Is your name ' + myName + '?'
If you're ahead of others, check out the MDN docs on strings.