So far, our site is static and has no interaction. There's no way to make our page do anything.
Our page just said hi. The box that popped up is called an alert.
Note the parentheses in our function. In programming, we call those parens. They're a good clue that the piece of code we're looking at is a function. The parens have a special use, which is to collect data that the function itself will then use. The information passed into parens is called an argument. In the case of our function here, we're giving it the argument
"alert" and the function
alert() use the same set of letters so we need a way to differentiate them. For a function, we use parens. For a piece of text, we use quotes. A piece of text contained within quotes is called a string. When we write code, everything that needs to be treated as a piece of text must be formatted as a string.
2 and the string
"2". The first is an integer while the second is a string. Let's see how this works.
Clear the contents of the right-hand panel in Codepen and add the following code:
alert(2 + 2); alert(4 * "2"); alert(8 + "2");
We will get the following alerts:
The first two make some sense. However, the third doesn't actually add the two numbers together.
"2"from a string to an integer and gave us the result. It inferred the data type.
8and tried to add the string
+. If we were to add
"trout"together, we would get
Try experimenting with different combinations of strings and integers. Guess what will happen before you refresh your page.
Lesson 4 of 7
Last updated May 20, 2021