Day 7: Consolidation 1
Lesson 1: Simple Data Types
Let's go back to the start so you can see just how far you have come!
Console.log()
You use console.log()
to print things to the console of the playground we have been working with. This also works in the browser.
Constants
You can create constants that are able to hold data that never changes.
1const pi = 3.14; 2const numberOfGoodMatrixFilms = 1;
Variables
If you want to store data that can change then variables are the way to go.
1var age = 30; 2var fishInTheSea = 156009304992;
But remember that you want to keep the variable locally scoped, in most cases and so using let
is best practice.
1let age = 32; 2let fishInTheSea = 239573939834;
Strings
If you want to store text, you can do so in a string data type.
1const name = "All The Code"; 2let yourName = "Coder";
There are a few ways to declare strings, you can use " "
, ' '
and ` `
. The first two, single and double quote, do the same thing however the third, back-tick, has some extra functionality that will get it's own lesson.
Strings, like everything else in JavaScript are objects and so they have useful functions, known as methods, that can be called on them. You can see a full list on the MDN Docs page. Here are a few examples of the most useful ones:
1const myString = " Hello World "; 2 3const lowerCased = myString.toLowerCase(); 4const upperCased = myString.toUpperCase(); 5const removedWhiteSpace = myString.trim(); 6const stringLength = myString.length; 7const manyStrings = myString.repeat(4);
In the above myString.length
doesn't have ()
that's because it's not a function to be called but a property to be read, like a value in any object you can just read it rather than having to call a function.
In the next lesson when we recap objects, we'll go into this more. As you learn further about functions and objects you will become more comfortable with lifting the lid on this topic.
Numbers
You should be very comfortable with numbers at this stage.
We can declare them
1let myAge = 30;
and change them
1let myAge = 30; 2myAge = myAge + 1;
All of the operators you can think of are available:
Addition
+
Subtraction
-
Division
/
Multiplication
*
One you've probably not heard of called 'modulo', that returns the remainder of one number divided by another using the %
symbol.
Boolean
Booleans are true
or false
and are used to store the result of comparison operations or simply act as a flag to tell you the binary state of something. Binary basically means two. True or false, on or off.
1let greaterThan = 4 > 3; 2let lessThan = 3 < 4; 3let greaterThanEqualTo = 4 >= 3; 4let lessThanEqualTo = 3 < 4; 5let equalTo = 3 == 3; 6let notEqualTo = 3 != 4; 7let exactlyEqualTo = 3 === 3;
All of the lines above will set the variable on the left of the first =
sign to be true because the test that they apply is true. If we change the first line to let greaterThan = 1 > 3;
then greaterThan
will be set to false because the 1 is not greater than 3.
For not equal to !=
we simply take the equals to operator ==
and replace the first =
with the not operator !
JavaScript has some quirks that test if things are truly equal to, this is when we should use three equals symbols ===
🤦♂️