- Now that we have operators to work with, let's look at how they interact with numbers and strings. In the previous examples, I used simple numbers to perform simple math operations. Var a equals four, var b equals five, var sum equals a plus b. That works fine as long as both parts of the equation is a number. But what happens when that's not the case? Take the following example. Var a still equals four, but var b is now the string five.
And var sum is still a plus b. Now, because one of the variables contains a string, javascript assumes you want to combine the two strings together and the plus symbol no longer works as an arithmetic operator, but as a string operator. The result we get is the string four five, or 45 which is the first variable followed by the second variable. Not at all four plus five. This is how we string different types of data together using javascript.
Using the plus symbol as a string operator, we add different content together to be treated as one long string. Any item included in such a string is treated like a string so a number will be converted to a string in this new context. The plus operator is unique in this regard. It's the only one of the arithmetic operators that is also a string operator, meaning you have to be careful about what type of data your variables contain when you use it.
If we use one of the other operators, say a minus b instead, the math works even though we have a number and a string. Javascript says you're trying to do math here and even though b is a string, I see it only contains a number so I'm going to assume you meant to use it as a number. The same thing happens when we do multiplication and division, as long as your string only contains a number, the math will work. It's sloppy and programmatically wrong, but you get results.
However, if you put anything other than a number in the string you're trying to subtract, multiply, or divide, bad things happen. If a is a number and b is a string and I try to say sum a minus b, then it returns NAN, or not a number. This means you're trying to do math using values that are not numbers, so if you ever seen NAN in your console, you know one or more of your variables contains a string.
Author
Updated
4/1/2019Released
5/17/2017Through practical examples and mini-projects, this course helps you build your understanding of JavaScript piece by piece, from core principles like variables, data types, conditionals, and functions through advanced topics including loops, closures, and DOM scripting. Along the way, you will also be introduced to some ES6 and the basics of JavaScript libraries.
- What is JavaScript?
- Working with data
- Using functions and objects
- Working with JavaScript and the DOM
- Changing DOM elements
- Handling events
- Working with loops
- Making images responsive using markup
- Troubleshooting code
- Validating functionality
- Minifying JavaScript
Skill Level Beginner
Duration
Views
Related Courses
-
Introduction
-
Welcome1m 7s
-
-
1. JavaScript: An Introduction
-
What is JavaScript?2m 38s
-
-
2. The Basics
-
Introducing the browser console10m 31s
-
3. Working with data
-
Data types in JavaScript4m 2s
-
Arrays2m 20s
-
4. Functions and Objects
-
Functions in JavaScript3m 28s
-
Build a basic function3m 29s
-
Anonymous functions5m 11s
-
Variable scope3m 17s
-
ES2015: let and const6m 12s
-
Make sense of objects3m 19s
-
Object constructors6m 16s
-
Closures8m 11s
-
-
5. JavaScript and the DOM, Part 1: Changing DOM Elements
-
Access and change elements4m 33s
-
Access and change classes3m 45s
-
Access and change attributes4m 53s
-
Add DOM elements6m 56s
-
6. Project: Create an Analog Clock
-
Use CSS to move clock hands3m 49s
-
7. JavaScript and the DOM, Part 2: Events
-
What are DOM events?1m 31s
-
Some typical DOM events1m 59s
-
Add and use event listeners6m 51s
-
-
8. Project: Typing Speed Tester
-
Rundown of HTML markup2m 58s
-
Build a count-up timer5m 56s
-
Add a reset button5m 3s
-
-
9. Loops
-
Loops3m 37s
-
Looping through arrays4m 7s
-
Break and continue loops7m 9s
-
-
10. Project: Automated Responsive Images Markup
-
Project breakdown1m 55s
-
Rundown of project setup3m 26s
-
-
11. Troubleshooting, Validating, and Minifying JavaScript
-
Troubleshooting JavaScript7m 20s
-
Online script linting5m 57s
-
Automate script linting8m 24s
-
Online script minification2m 50s
-
Automate script minification2m 24s
-
12. Bonus Chapter: Ask the Instructor
-
What are arrow functions?3m 11s
-
What does the % symbol do?3m 47s
-
-
Conclusion
-
Next Steps1m 55s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Working with strings and numbers