So Many Operators! JavaScript operators are symbols or keywords used to perform operations values. They allow you to manipulate data and make decisions in your JavaScript code. You have already used the typeof
keyword operator and the =
assignment of value symbol, but there are a lot more operators! In this section, we will focus on Arithmetic, Comparison, and Logical Operators.
Arithmetic Operators¶
These operators perform basic mathematical operations, for numeric calculations, and also to join strings together.
Arithmetic Operators with Strings:
-
Concatenation with
+
Operator: The most common arithmetic operation with strings is concatenation, achieved using the+
operator. When used between strings, it concatenates or joins them. For example,"Hello " + "World"
results in"Hello World"
. -
Other Arithmetic Operators: Operations like
-
,*
,/
, etc., on strings typically result inNaN
(Not a Number), as these operations don't have a logical string equivalent. For example,"Hello" - "World"
results inNaN
.
Comparison Operators¶
Comparison operators compare values and return a Boolean (true
or false
). They are used to make decisions and control the flow of your code. You will study flow control in the next module.
Comparison Operators with Strings:
-
Alphabetical Ordering: Comparison operators (
<
,>
,<=
,>=
) are used to compare strings based on standard lexicographical ordering, using Unicode values. This means strings are compared alphabetically, and character by character. For example,"Apple" < "Banana"
evaluates totrue
because"Apple"
comes before"Banana"
in alphabetical order. "A" comes before "B" in the alphabet and in Unicode.Unicode is a universal character encoding standard that provides a unique number for every character, regardless of the platform, program, or language. This comprehensive system is designed to represent text in all the written languages of the world, as well as many symbols, emojis, and historic scripts. Unicode covers a vast range of characters and assigns each one a code point, which is a number in the format U+XXXX, where "XXXX" represents a hexadecimal value. For instance, the Unicode value for the uppercase letter "A" is U+0041 and "B" is represented by the code point U+0042. This standardization allows for the consistent encoding, representation, and handling of text, ensuring that digital text is accessible and consistent across different systems and devices. JavaScript strings are Unicode-based, allowing them to include characters from virtually any writing system.
-
Equality Operators: The
==
and===
operators can be used to check if two strings are exactly the same. For instance,"Hello" === "Hello"
results intrue
. -
Case Sensitivity: It's important to note that these comparisons are case-sensitive, meaning
"a"
and"A"
are considered different (with"A"
typically coming before"a"
in Unicode order).
In summary, while arithmetic operations with strings are limited to concatenation, comparison operators open up a broad range of possibilities, allowing for alphabetical sorting and ordering based on Unicode values. This flexibility makes string manipulation in JavaScript both powerful and convenient.
Logical Operators¶
Logical operators are used to combine or manipulate Boolean values. They include AND (&&
), OR (||
), and NOT (!
) to make logical decisions in your code.
Now let's look at some code examples that combine many of these operators:
JAVASCRIPT
Practice Problems¶
Objective: Write JavaScript code to evaluate som values using arithmetic, comparison, and logical operators.
-
Declare two variables,
x
andy
, and assign them integer values. Example:let x = 5; let y = 7;
-
Choose an arithmetic operation for
x
. It could be squaring it, doubling it, etc. Example: Squarex
. -
Choose a comparison operation for
y
. It could be checking ify
is greater than, less than, or equal to a certain value. Example: Check ify
is greater than 10. -
Use a logical operator (
&&
or||
) to combine the conditions from steps 2 and 3. Example: Use logical AND (&&
) to combine the conditions. -
Write the final expression that combines the arithmetic operation on
x
, the comparison operation ony
, and the logical operator. Example:(x * x < 30) && (y > 10)
-
Store the result of the expression in a variable and log the result to the console. Example:
let result = (x * x < 30) && (y > 10); console.log(result);
-
Add a comment describing what the code is checking. Example:
// Check if x squared is less than 30, and also check if y is greater than 10.
Answer:
Practice With AI¶
Use the prompt below to get even more practice problems from any Large Language Model / AI of your choice:
Act as my JavaScript tutor. I am a beginner learning about arithmetic, comparison, and logical operators in JavaScript for the first time. Generate 10 practice problems that involve using these operators based on the extracurricular interest or hobby of [insert your interest or hobby here]. Only provide the problem statements, not the answers.
🎉🎉🎉 Congrats, you've completed your third lesson! When you're ready, you can move on to the next lesson. 🎉🎉🎉