Sample Video Frame

Created by Zed A. Shaw Updated 2024-06-20 22:54:43

03: Simple Math and Strings

In this code sample you will learn what a string is and also how to do some simple math, as well as use a little bit of the Math library. As usual, you are to type this code in, get it working, and study the code for additional information on these concepts. You should also attempt to break this code and change it in some way that you find interesting.

The Code

View Source file code.js Only

// You can give multiple things for console.log to print.
// Separate each thing with a comma.
console.log("Name:", 'Zed A. Shaw');
console.log("Age:", 43);
console.log("Feet Tall:", Math.floor(74 / 12));
console.log("And Inches:", 74 - (Math.floor(74 / 12) * 12));
console.log("Age * Height:", 43 * 74);
console.log("Age * Feet:", 43 * Math.floor(74 / 12));

In JavaScript there are a few ways to create a string. You can create a string with a single–quote 'Like this', with a double-quote "Like this", and in another way that you will learn about later in the course. Math and JavaScript use the same rules as your algebra class, where you used the acronym PEMDAS to figure out the order of operations. Remember that PEMDAS is more like PE(MD)(AS), meaning you do the parentheses, followed by exponents, followed by multiplication and division in order, followed by addition and subtraction in order. In some countries they use a different acronym for this, but it should be similar as math tends to be very universal.

What You Should See

As before when I run this I get the following:

Name: Zed A. Shaw
Age: 43
Feet Tall: 6
And Inches: 2
Age * Height: 3182
Age * Feet: 258
Previous Lesson Next Lesson

Register for Learn JavaScript the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.