Skip to content

Latest commit

 

History

History
217 lines (212 loc) · 3.11 KB

5. JavaScript Operators and Arithmetic operators.md

File metadata and controls

217 lines (212 loc) · 3.11 KB

5. JavaScript Operators and Arithmetic operators

Addition Operator

const x = 8;
const y = 4;
const result = x + y;
console.log(result);

Output

12
const x = 8;
const y = 4;
console.log(x + y);

Output

12

Float Values

const x = 8.5;
const y = 4;
const result = x + y;
console.log(result);

Output

12.5

Addition with Strings

const text1 = 'JavaScript';
const text2 = 'Programming';
console.log(text1 + text2);

Output

JavaScriptProgramming

Subtraction Operator

const x = 8;
const y = 4;
const result = x - y;
console.log(result);

Output

4

Multiplication Operator

const x = 8;
const y = 4;
const result = x * y;
console.log(result);

Output

32

Division Operator

const x = 8;
const y = 4;
const result = x / y;
console.log(result);

Output

2

Modulus Operator

const x = 8;
const y = 4;
const result = x % y;
console.log(result);

Output

0
const x = 11;
const y = 4;
const result = x % y;
console.log(result);

Output

3

Increment Operator

let x = 8;
console.log(++x);

Output

9
let x = 8;
console.log(x++);

Output

8
let x = 8;
console.log(x++);
console.log(x);

Output

8
9

Decrement Operator

let x = 8;
console.log(--x);

Output

7
let x = 8;
console.log(x--);
console.log(x);

Output

8
7

Exponentiation Operator

let x = 8;
const result = x ** 2;
console.log(result);

Output

64

Multiple Operators

const result = 4 / 2 + 3 * 5 - 1;
console.log(result);

Output

16

Practical Examples

const initialFee = 4535;
const discountPercent = 10;

const discountAmount = discountPercent/100 * initialFee;
const totalFee = initialFee - discountAmount;

console.log(`Fee after discount: ${totalFee} dollars`);

Output

Fee after discount: 4081.5 dollars

const kmDistance = 5;

const converstionRatio = 0.621371;
const milesDistance = kmDistance * converstionRatio;

console.log('You covered ' + milesDistance + ' miles.');

Output

You covered 3.106855 miles.

Programming Task

Create a program that converts degree celsius to fahrenheit. The formula to convert celsius to fahrenheit is Formula: fahrenheit = celsius * 1.8 + 32. What you can do is store the value of celsius value in the celsius variable. Then use the formula fahrenheit = celsius * 1.8 + 32 to convert celsius to fahrenheit and display the result.

Solution:

let celcius = 88;
let fahrenheit = celcius * 1.8 + 32;
console.log(`${celcius} celcius equals to ${fahrenheit} fahrenheit`);

Output

88 celcius equals to 190.4 fahrenheit

Programiz Quiz

Q. What is the output of the following code?

console.log(5**3);
  1. 555
  2. 15
  3. 125
  4. 8

Answer: 3