Day 4 of learning JavaScript: numbers vs. strings + DOM
Today I learned a few new things about JavaScript and also made a little bit of progress in building the people counter app and also learned about the DOM: The document object model. To explain the DOM briefly, it's just how you use JavaScript to modify a website.
I created a new button called "SAVE" and also added the attribute onclick="save()" so that a click on the button executes the also newly added function save() in JavaScript. I did the things exactly like I did them previously with the increment button. Now, to go further into the project I had to learn about strings because when I click on the Save button, a paragraph should appear that says: "Previous entries: count1 - count2 - count3 -" and so on.
You define a string like this in JavaScript:
let myString = "Hello World!" // the quotation marks are important!
If you log this variable out, the console should say "Hello World!".
Now what is the difference between strings and numbers? And also what happens if you combine them?
console.log(2 + 2) //does the mathematical operation. Logs out "4"
console.log(2 + "10") //cant do the math because of the string. Logs out "210".
console.log("10" + 2) //cant do the math as well. Logs out "102".
console.log("100" + "40") //only strings => they're just written as text. Logs out "10040".
As you see, numbers can execute mathematical operations and strings are just written as text. If you want to combine these 2 data types, the string takes over the control, because it can't do a mathematical operation. So the string just gets printed right next to the number.
I hope you learned something valuable from this post. The app should be done soon.