JavaScript features another type of data called Booleans
Unlike strings and numbers, Booleans can only be one of two values: true or false.
We can use comparison operators to check the relationship between two pieces of data:
This also works with strings and variables:
Booleans are super useful because they let us check if a condition has been reached.
We can check if a Boolean is true or false using an if/then statement.
In this example, let’s create a variable that tracks the number of user clicks.
We’ll increase this variable by 1 every time the button is pressed.
We’ll use an "if" statement to detect once the variable is 5, meaning the button has been clicked 5 times.
We can use the "else" statement to do something when the condition isn’t true.
There’s one more basic type of data that’s super useful: arrays.
Arrays store other pieces of data, like strings, numbers, and other arrays.
Example: var myFirstArray = ["Hello", "What’s up?", 0, 100, 200];
To access an item in an array, we say the array's name followed by a number in square brackets.
The first item of an array is actually the 0th item of the array.
Example: myFirstArray[0] == "Hello"
Example: myFirstArray[4] == 200
Let’s put this all together now!
We’ll create an array with a few lines of text in it.
Clicking on a button switches to the next line of text.
We have a problem! If we click too many times, we get an "undefined" error.
This is because we tried to access a value outside of the range of the array.
The array is 4 items long, so mySecondArray[4] results in undefined.
We can fix this by checking if we’ve reached the end of the array.
Writing __arrayName___.length gives us the length of an array.
Now, we can revise our function to include an "if" statement.
Once we reach the end of the array, let’s set our position back to 0.