Last active
March 14, 2023 20:59
-
-
Save sandrabosk/40b9b755ff157e672b7b7451699aeec2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // THIS FILE ALSO HAS EXAMPLE OF HOW TO USE MATH.FLOOR(), MATH.ROUND() AND MATH.CEIL() AND TOFIXED() (FROM THE LESSON ADVANCED NUMBERS) | |
| There are two kinds of data types in JavaScript: | |
| - primitives or primitive values and | |
| - objects or non-primitive values. | |
| a primitive (a.k.a. primitive value or primitive data type) is any data that is not an object and has no methods | |
| There are 6 primitive data types: | |
| - number, | |
| - string, | |
| - boolean, | |
| - null, | |
| - undefined, | |
| - symbol | |
| NUMBER AS A DATATYPE | |
| - integers and | |
| - floating-point numbers | |
| const age = 34; | |
| const price = 12.99; | |
| Special numeric values: NaN and Infinity | |
| NaN stands for Not a Number and it represents a computational error. | |
| const name = 'Sandra'; // <== string data type | |
| const whatIsThis = name / 2; | |
| console.log(whatIsThis); // ==> NaN | |
| NaN is not a normal number, although it belongs to this data type. | |
| If you get NaN, you are probably performing the operation on a string or some other data type that isn’t a number. | |
| NUMBER EXPRESSIONS | |
| Basic math operations: | |
| + addition | |
| - subtraction | |
| * multiplication | |
| / division | |
| ** exponentiation | |
| % modulo (is the remainder operator. Think of this as saying If I divide the first number by the second, what is the remainder?) | |
| exponentiation: | |
| console.log(2 ** 5); | |
| // 2 * 2 * 2 * 2 * 2 | |
| // => 32 | |
| modulo: | |
| console.log(22 % 3); // 1 | |
| console.log(26 % 4); // 2 | |
| console.log(26 % 4 === 3); // false | |
| console.log(24 % 4 === 0); // true | |
| STRINGS | |
| // 3 ways to create strings: | |
| // double (""), single quotes ('') and backticks (``) | |
| // backticks (``) --> template literals: strings that allow us to embed expressions in them | |
| let name = 'Ana'; | |
| console.log(`Hello there, ${name}!`); | |
| // ==> Hello there, Ana! | |
| console.log(`${name} has ${22+12} years.`); | |
| // ==> Ana has 34 years. | |
| const statement = 'this is our test string'; | |
| console.log(`This is the first letter - 1: ${statement[0]}`); | |
| // the same as above ^^^^ | |
| console.log("This is the first letter - 2:", statement[0]); | |
| // the same as above ^^^^ | |
| console.log("This is the first letter - 3:" + statement[0]); | |
| statement[0] = 'T'; | |
| console.log(`Is string changed: ${statement}`); // Is string changed: this is our test string | |
| let num = 3; | |
| console.log(typeof num); | |
| num += 11; // num = num + 11 // number | |
| console.log(`num 1: ${num}`); // 14 | |
| num = '3'; // now our num is type of string so math operations will "fail" | |
| console.log(typeof num); // string | |
| // adding to a string => concatenation | |
| num += '11'; // num = num + 11 => string | |
| console.log(`Num 2: ${num}`); // 311 | |
| // length is not a method | |
| console.log(`How long is this string: ${statement.length}`); // 23 | |
| // ✅ check if string includes substring or character: | |
| // includes() ===> returns true or false | |
| // indexOf() ===> returns the position where character is found or -1 if not found | |
| console.log(`CHECK INCLUDES: ${statement.includes('stri')}`); // true | |
| console.log(`CHECK INDEXOF: ${statement.indexOf('string ')}`); // -1 (false) | |
| console.log(`CHECK INDEXOF: ${statement.indexOf('string')}`); // 17 | |
| // ✅ access character in the string charAt(index) | |
| console.log(statement.charAt(0)); // t | |
| // ‼️ string methods never mutate the string | |
| // substring(start, end] - the last one is not included (5, 9) --> counts till 8 | |
| // substring's parameters are reversible, as it will always use its smallest parameter value as the start index and largest value as the stop index. | |
| // substring will treat a negative start index as 0. | |
| let substring1 = statement.substring(5, 10); // we count from 0 | |
| console.log(substring1); // is ou ==> the last is not included, meaning the "end" is not inclusive | |
| let substring2 = statement.substring(5, 11); | |
| console.log(substring2); // is our | |
| let substring3 = statement.substring(-11, 5); // negative number is zero for substring(), so this is the same as (0, 5) | |
| console.log(`hello: ${substring3}`); // as if starts with zero ==> hello: this | |
| let substring4 = statement.substring(5); // from this position all the way till the end of the string if we don't pass the end value | |
| console.log(`What if we pass only start to substring: ${substring4}`); // What if we pass only start to substring: is our test string | |
| ------- | |
| // substr(start, howManyFromStart) | |
| let substr1 = statement.substr(5, 11); | |
| console.log(substr1); // is our test | |
| let substr2 = statement.substr(5, 9); | |
| console.log(substr2); // is our te | |
| ------ | |
| // slice(start, end] - last one is not inclusive and can accept negative numbers (counts from the last index, from the end) | |
| let slice1 = statement.slice(5, 10); // we count from 0 | |
| console.log(slice1); // is ou (the same output as in substring) | |
| let useSliceWithNegative = str.slice(-4); | |
| console.log(useSliceWithNegative); // ring | |
| ------- | |
| // startsWith() method - determines whether a string begins with the characters of a specified string, | |
| // returns true or false as appropriate | |
| const str1 = 'To be, or not to be, that is the question.'; | |
| console.log(str1.startsWith('To be')); // true | |
| console.log(str1.startsWith('not to be')); // false | |
| console.log(str1.startsWith('not to be', 10)); // true | |
| // ************************************************************ | |
| // endsWith() method - determines whether a string ends with the characters of a specified string | |
| // returns true or false as appropriate. It’s also case-sensitive. | |
| console.log(str1.endsWith('question.')); // true | |
| console.log(str1.endsWith('to be')); // false | |
| console.log(str1.endsWith('to be', 19)); // true | |
| // ************************************************************ | |
| ADVANCED NUMBERS | |
| An example that demonstrates the differences between Math.floor(), Math.round(), and Math.ceil(): | |
| let num1 = 3.2; | |
| let num2 = 5.7; | |
| console.log(Math.floor(num1)); // 3 | |
| console.log(Math.floor(num2)); // 5 | |
| console.log(Math.round(num1)); // 3 | |
| console.log(Math.round(num2)); // 6 | |
| console.log(Math.ceil(num1)); // 4 | |
| console.log(Math.ceil(num2)); // 6 | |
| In this example, num1 has a decimal value of 0.2, which means that: | |
| - Math.floor() will round it down to 3, while | |
| - Math.round() will also round it down to 3. | |
| - Math.ceil() will round it up to 4. | |
| On the other hand, num2 has a decimal value of 0.7, which means that | |
| - Math.floor() will round it down to 5, | |
| - Math.round() will round it up to 6, and | |
| - Math.ceil() will also round it up to 6. | |
| TOFIXED() | |
| toFixed() is a method that converts a number to a string with a specified number of decimal places | |
| let num = 3.14159265359; | |
| let roundedNum = num.toFixed(2); | |
| console.log(roundedNum); // Output: "3.14" | |
| toFixed() returns a string, so if you need to perform further calculations with the rounded number, | |
| you'll need to convert it back to a number first using the Number() function. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment