You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.2 KiB
JavaScript

1 year ago
console.log("Hello B322!");
// [SECTION] Arithmetic Operators
let x = 1397;
let y = 7831;
let sum = x + y;
console.log("Result of addition operator: " + sum);
let difference = x - y;
console.log("Result of subtraction operator: " + difference);
let product = x * y;
console.log("Result of multiplication operator: " + product);
let quotient = x / y;
console.log("Result of division operator: " + quotient);
let remainder = y % x;
console.log("Result of modulo operator: " + remainder);
// [SECTION] Assignment Operators
// Basic Assignment Operator (=)
// The assignment operator assigns the value of the **right** operand to a variable.
let assignmentNumber = 8
// Addition Assignment Operator (+=)
// The addition assignment operator adds the value of the right operand to a variable and assigns the result to the variable.
assignmentNumber = assignmentNumber + 2;
console.log("Result of addition assignment operators: " + assignmentNumber);
assignmentNumber += 2;
console.log("Result of addition assignment operator: " + assignmentNumber);
// Subtraction/Multiplication/Division Assignment Operator (-=,*=,/=);
assignmentNumber -= 2;
console.log("Result of subtraction assignment operator: " + assignmentNumber);
assignmentNumber *= 2;
console.log("Result of multiplication assignment operator: " + assignmentNumber);
assignmentNumber /= 2;
console.log("Result of division assignment operator: " + assignmentNumber);
// Multiple Operators
let mdas = 1 + 2 - 3 * 4 / 5;
console.log("Result of mdas operation: " + mdas);
/*
1. 3 * 4 = 12
2. 12 / 5 = 2.4
3. 1 + 2 = 3
4. 3 = 2.4 = 0.6
*/
// pemdas
let pemdas = 1 + (2 - 3) * (4 / 5);
console.log("Result of pemdas operation: " + pemdas);
// The operations were done in the following order
/*
1. 4/5 = -0.8
2. 2-3 = -1
3. 1 + -1 = 0
4. 0 * 0.8 = 0
*/
// Increment and Decrement
// Operators that add or subtract values by 1 and reassigns the value of the variable where it was added to.
let z = 1;
// The value of "z" is added by a value of one before returning the value and storing it in the variable "increment"
let increment = ++z;
console.log("Result of pre-increment: " + increment);
// The value of "z" was also increased even though we didn't implicitly specify any value reassignment.
console.log("Result of pre-increment: " + z);
// The value of "z" is returned and stored in the variable "increment" then the value of "z" is increased by one increment
increment = z++;
// The value of "z" is at 2 before it was incremented
console.log ("Result of post-increment: " + increment);
// The value of "z" was increased again reassigning the value to 3
console.log("Result of post-increment: " + z);
let decrement = --z;
console.log("Result of pre-decrement: " + decrement);
console.log("Result of pre-decrement: " + z);
decrement = z--;
console.log("Result of post-decrement: " + decrement);
console.log("Result of post-decrement: " + z);
// Coercion
// Type coercion is the automatic or implicit conversion of values from one data type to another
// This happens when operations are performed on different data types that would normally not be possible and yield irregular results
// Values are automatically converted from one data type to another in order to resolve operations
let numA = '10';
let numB = 12;
// Adding/concatenating a string and a number will result in a string
// This can be proven in the console by looking at the color of the text displayed
// Black text means that the output returned is a string data type.
let coercion = numA + numB;
console.log(coercion);
console.log(typeof coercion);
let numC = 16;
let numD = 14;
let nonCoercion = numC + numD;
console.log(nonCoercion);
console.log(typeof nonCoercion);
let numE = true + 1;
console.log(numE);
let numF = false + 1;
console.log(numF);
console.log(typeof numF);
// [SECTION] Comparison Operators
// Equality Operator (==)
// - checks whether the operands are equal/have the same content
// - it returns a boolean value
let juan = 'juan';
console.log(1 == 1);
console.log(1 == 2);
console.log(1 == '1');
console.log('juan' == 'juan');
console.log('juan' == juan);
// Inequality Operator (!=)
// -checks wheter the operands are not equal/have different content
console.log(1 != 1);
console.log(1 != 2);
console.log(1 != '1');
console.log('juan' != 'juan');
console.log('juan' != juan);
// Strict Equality Operator
// Checks whether the operands are equal/have the same content
// Also COMPARES the data types of 2 values.
console.log(1 === 1);
console.log(1 === 2);
console.log(1 === '1');
console.log('juan' === 'juan');
console.log('juan' === juan);
// Strict Inequality Operator
// Checks whether the operands are not equal/have the same content
// Also COMPARES the data types of 2 values
console.log(1 !== 1);
console.log(1 !== 2);
console.log(1 !== '1');
console.log('juan' !== 'juan');
console.log('juan' !== juan);
// Relational Operators
// Some comparison operators check whether one value is greater or less than to other value
let a = 50;
let b = 65;
// GT or Greater Than operator ( > )
let isGreaterThan = a > b;
// LT or Less Than operator ( < )
let isLessThan = a < b;
// GTE or Greater Than or Equal Operator ( >= )
let isGTorEqual = a >= b;
// LTE or Less Than or Equal Operator ( <= )
let isLTorEqual = a <= b;
console.log(isGreaterThan);
console.log(isLessThan);
console.log(isGTorEqual);
console.log(isLTorEqual);
let numStr = "30";
console.log(a > numStr);
// Coercion - forced coercion to change the string to a number.
console.log(b <= numStr);
let str = "twenty";
console.log(b >= str);
// This results to NaN.
// Logical Operators
let isLegalAge = true;
let isRegistered = false;
// Logical And Opertaor (&& - Double Ampersand)
// Returns true if all operands true
let allRequirementsMet = isLegalAge && isRegistered;
console.log("Result of logical AND Operator: " + allRequirementsMet);
// Logical Or Operator (|| - Double Pipe)
// Returns true if one of the operands are true
let someRequirementsMet = isLegalAge || isRegistered;
console.log("Result of logical OR Operator: " + someRequirementsMet);
// Logical Not Operator (! - Exclamation Point)
// Returns the opposite value
let someRequirementsNotMet = !isRegistered;
console.log("Result of logical NOT Operator: " + someRequirementsNotMet);