Learning to Code: Day 38 — Basic JavaScript Part 3

Hugh Burgess
2 min readNov 11, 2020

Hello everyone, I’m starting night shifts now at work because of COVD-19 so these coding days have been a bit sparse of late, however I am learning at my pace, not that of someone else, so this works for me, to code in the evening a couple hours before work.. Here we go, back into ye ole’ JavaScript! We’ll be parcing through a couple more math terms today. Thanks to FreeCodeCamp for the lessons.

Finding the Remainder in JS

The remainder operator (“%”) is used to check a number in JS as even or odd, when the remainder is checked against the number 2. Odd numbers will result in an answer of 1 whereas even numbers will give a null output:

48 % 2 = 0 (Even)

17 % 2 = 1(Odd)

Compound Assignment with Augmented Maths

Addition

When using assignments to modify the contents of a variable, remember that everything to the right of the equals sign is evaluated first, and if we want to do addition as well as an assignment, we can use the += operator to do both simultaneously. So if we say:

var myVar =1;

myVar += 5;

We are defining the variable “myVar” and to be equal to 1, then we add 5 to that variable, and perform the following to give the result which will equal 6:

console.log(myVar);

Subtraction

The same can be applied with subtraction operations by using -= instead of +=. For example:

var myVar = 1;

myVar -= 5;

console.log(myVar);

This will result in -4.

Multiplication

If we want to multiply and perform an assignment at the same time, we can use the *= operator. Same process of thought as before.

Division

You guessed it, we can use the /= operator for division and assignment simultaneously. Need I say more.

Declaring String Variables

Strings are letters instead quotations, and for example “hello” would be called a string literal. We can type whatever we want in a string, but we must be cautious for some other types of wording..

Escaping Quotations in Strings

Everything has a meaning in programming, even quotes within strings. They may be misread by JS, ah I miss HTML. So simple. If we want to make a quote within a string without alerting JS that we have reached the end of the string, we have to use a nifty little move and type a backslash before the quote:

var simpleString = “Then he looked, \“I have had a great day today\”, said John.”;

Note: It’s good practise to use double quotes ( “ ” )for a string and not single quotes ( ‘ ’ ) as you may need to use the single quotes in words like “it’s” for example. If you do so, always remember that backslash: “it\’s”.

Aaaaaand let’s call it there for now. Looking forward to next time. Cheers guys!

--

--