Basic Concepts
Chapter: 7
Math operators
Arithmetic Operators
The name might be a bit of a giveaway but, Arithmetic operators pretty perform arithmetic functions on numbers (both literals and variables).
var x = 10 + 5;
document.write(x);
document.write(x);
JS
var x = 10;
var y = x + 5 + 22 + 45 + 6548;
document.write(y);
var y = x + 5 + 22 + 45 + 6548;
document.write(y);
JS
Heads up!
You can get the result of a string expression using the eval() function, which takes a string expression argument like eval("10 * 20 + 8") and returns the result. If the argument is empty, it returns undefined.
You can get the result of a string expression using the eval() function, which takes a string expression argument like eval("10 * 20 + 8") and returns the result. If the argument is empty, it returns undefined.
Arithmetic Operators
What will the following statements display? var test=5+7; document.write(test);
Multiplication
Want to hear a joke?
What tool is best suited for math?...Multi-pliers!
JavaScript is pretty good at math too though!
We use the * operator to multiply one number by the other.
Like this:
var x = 10 * 5;
document.write(x);
document.write(x);
JS
Heads up!
10 * '5' or '10' * '5' will give the same result. But trying to multiply a number with string values that aren’t numbers, like 'sololearn' * 5 will return NaN (Not a Number).
10 * '5' or '10' * '5' will give the same result. But trying to multiply a number with string values that aren’t numbers, like 'sololearn' * 5 will return NaN (Not a Number).
Multiplication
What character do we use for multiplication?
Division
What character do we use for division?
The Modulus
Time to talk remainders. You hated them in school, but they’re pretty easy here, promise.
The Modulus (%) operator returns the division remainder (what’s left over).
Like this:
var myVariable = 26 % 6;
JS
Heads up!
In JavaScript, we can use the modulus operator on integers AND on floating point numbers.
In JavaScript, we can use the modulus operator on integers AND on floating point numbers.
The Modulus
What’s the result of using a modulus operator for 38%5?
Increment & Decrement
"Wait I've heard of an increment, but what the heck is a decrement?" We hear some of you say. Well, throw an increment into reverse and presto, you got yourself a decrement. Let’s dig a little deeper...
Increment ++
The increment operator increases the numeric value of its operand by 1. When placed before the operand, it’ll return the incremented value. When placed after it, it’ll return the original value and then increments the operand.
Decrement --
The decrement operator decreases the numeric value of its operand by 1. When placed before the operand, it’ll return the decremented value. When placed after the operand, it’ll return the original value and then decrements the operand.
Some examples:
Heads up!
Just like the math you learned in school, you can change the order of the arithmetic operations by using parentheses.
Like this: var x = (100 + 50) * 3;
Just like the math you learned in school, you can change the order of the arithmetic operations by using parentheses.
Like this: var x = (100 + 50) * 3;
Increment & Decrement
What are increment and decrement are used for?
Practice:
Math Operators
In the office, 2 monitors are connected to each computer.
The first line of the given code takes the number of computers as input.
Task
Complete the code to calculate and output the number of monitors to the console
Note: Comment down your answer for this practice set.
Sample Input
10
Sample Output
20
Hint
Since each computer has 2 monitors, simply multiply the count of the computers by 2.
Use the multiplication operator (*).
Chapter: 8
Assignment Operators
Assignment Operators
Next in a series very logically named operators is...
Assignment operators!
And you guessed it, we use these guys to assign values to JavaScript variables.
Heads up!
You can use multiple assignment operators in one line, such as x -= y += 9.
You can use multiple assignment operators in one line, such as x -= y += 9.
Assignment Operators
Calculate and enter the resulting value of this expression: var number = 20; number *= 5;
Chapter: 9
Comparison Operators
Comparison Operators
We can use comparison operators in logical statements to find out if variables or values are different.
You get either true or false.
For example, the equal to (==) operator checks whether the operands' values are equal.
var num = 10;
console.log(num == 8);//outputs false
console.log(num == 8);//outputs false
JS
Heads up!
You can compare all types of data with comparison operators, they’ll always return true or false.
You can compare all types of data with comparison operators, they’ll always return true or false.
Comparison Operators
Check out this table to see a breakdown of comparison operators.
Heads up!
One important thing to remember when we use operators, is that they only work when they’re comparing the same data type; numbers with numbers, strings with strings, you get the idea.
One important thing to remember when we use operators, is that they only work when they’re comparing the same data type; numbers with numbers, strings with strings, you get the idea.
Comparison Operators
Enter the corresponding operators according to the comments at right.
Practice:
Math Operators
There are a lot of situations where you want to check someone’s age...not just at the bar!
You are given a program that takes the age of the user as input.
Complete the code to check if the user is an adult, and output to the console the corresponding boolean value.
Sample Input
20
Sample Output
true
Note: Comment down your answer for this practice set
If the user is 18 or older, they’re considered an adult.
console.log(20>18) outputs true.
There are a lot of situations where you want to check someone’s age...not just at the bar!
You are given a program that takes the age of the user as input.
Complete the code to check if the user is an adult, and output to the console the corresponding boolean value.
Sample Input
20
Sample Output
true
If the user is 18 or older, they’re considered an adult.
console.log(20>18) outputs true.
console.log(20>18) outputs true.
Chapter: 10
Logical or Boolean Operators
Logical Operators
Logical Operators, also known as Boolean Operators, (or the Vulcan Operators….ok, that one isn't true) evaluate an expression and return true or false.
Check out the table below to see more details on the logical operators (AND, OR, NOT).
Heads up!
You can check all types of data; comparison operators always return true or false.
You can check all types of data; comparison operators always return true or false.
Logical Operators
Logical AND (&&) returns true if:
Logical Operators
Let's take a look at an example. Here we’ve connected two Boolean expressions with the AND operator.
(4 > 2) && (10 < 15)
JS
- The first condition determines whether 4 is greater than 2, which is true.
- The second condition determines whether 10 is less than 15, which is also true.
Ta da! The whole expression is true...very logical!
Conditional (Ternary) Operator
Conditional, or Ternary, operators assign a value to a variable, based on some condition.
This is what the syntax would look like:
variable = (condition) ? value1: value2
JS
var isAdult = (age < 18) ? "Too young": "Old enough";
JS

Noon Or Midnight
Given a clock that measures 24 hours in a day, write a program that takes the hour as input and ...
PRO
Heads up!
With logical operators you can connect as many expressions as you want or need to.
With logical operators you can connect as many expressions as you want or need to.
Logical NOT returns true, if:
Practice:
Logical or boolean operators
Time flies when you’re having fun.
Given a clock that measures 24 hours in a day, write a program that takes the hour as input. If the hour is in the range of 0 to 12, output am to the console, and output pm if it's not.
Sample Input
13
Sample Output
pm
Note: Comment down your answer for this practice set.
Assume the input number is positive and less than or equal to 24.
Chapter: 11
String Operators
String Operators
Time to introduce the most useful operator for strings...drum roll please.
...Concatenation.
We can use concatenation (represented by the + sign) to build strings made up of multiple smaller strings, or by joining strings with other types. Check it out:
var mystring1 = "I am learning ";
var mystring2 = "JavaScript with SoloLearn.";
document.write(mystring1 + mystring2);
var mystring2 = "JavaScript with SoloLearn.";
document.write(mystring1 + mystring2);
JS
Heads up!
Numbers in quotes are treated as strings: So "42" is not the number 42, it’s a string that includes the two separate characters, 4 and 2.
Numbers in quotes are treated as strings: So "42" is not the number 42, it’s a string that includes the two separate characters, 4 and 2.
String Operators
What’s the output of the following code? var x = "50"; var y = "100"; document.write(x+y);
Chapter: 12
Module 2 Quiz
in the data types of the data shown below in the comments field:
What's the result of the expression var1&&var2, if var1=true and var2=false?
Chapter: 13
Code Project: Trip Planner
Trip Planner
You need to plan a road trip. You are traveling at an average speed of 40 miles an hour.
Given a distance in miles as input (the code to take input is already present), output to the console the time it will take you to cover it in minutes.
Sample Input:
150
Sample Output:
225
Explanation: It will take 150/40 = 3.75 hours to cover the distance, which is equivalent to 3.75*60 = 225 minutes.
Comments
Post a Comment