Beginner JavaScript – Chapter 5 – Expressions

Beginner JavaScript – Chapter 5 – Expressions

Note:

As stated in my initial post, this is part of a series of JavaScript-related posts. As I am becoming knowledgeable in JavaScript, I am posting out what I’ve learned, to re-enforce my knowledge, as well as maybe helpful other web designers/developers in learning JavaScript. If I have mis-stated anything, please feel free to post it in the comments. You’ll help me, and others in learning JavaScript. Thanks!

Expressions are operations on numbers or text strings.

Numeric Expressions

On numbers, you typically perform arithmetic operations, such as addition, subtraction, multiplication, division, etc. To code an arithmetic expression, you use arithmetic operators to operate on two or more values.

Examples of arithmetic operations:

4 + 3
9 * 5
12 / 2

Arithmetic Operators

  • Addition (+): 2 + 3
  • Subtraction (-): 5 – 7
  • Multiplication (*) : 8 * 4
  • Division (/): 12 / 6
  • Modulus (%): 12 % 5
    This operator calculates the remainder of two numbers. Example: 13 % 4 is 1

Two other operators used to increment/decrement are:

  • Increment: (++): counter++
  • Decrement (–): counter–

Numeric expressions follow an order of precedence, based on the order.

  1. Increment Operator (++)
  2. Decrement Operator (–)
  3. Multiplication (*), Division (/) & Modulus (%)
  4. Addition (+), Subtraction (-)

Another way to create precedence is to group expressions with parenthesis.

2 + 4 * 6 = 26 Multiplication is performed first, followed by addition.
(2 + 4) * 6 = 48 Addition grouped by parenthesis is performed first, 
followed by multiplication.

String Expressions

The purpose of string expressions is to join, or concatenate text.

Examples of string operations:

"Dennis " + "Deacon"
"Go to the store and bring me some " + "ice cream."