Created
December 24, 2018 06:56
-
-
Save shoaibk-dev/1fad87cf7d59839d54f3668a9c985c8e 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
| // statically typed - type is enforced | |
| // dynamically typed - infer variables at runtime | |
| // weakly typed - allow type to be inferred as another type [1 + '2' = '12' => it coerce number into string] | |
| // type coercion - process of converting value from one type to another | |
| // type coercion can be imlicit[operators to value of different types] or explicit[type casting - Number(value)] | |
| // === strict equality operator => does not trigger implicit type coercion [both type and value we're compairing have to be the same] | |
| // == loose equality operator on the other hand does both comparision and type coercion [77 == '77' => true - perform type coercion] | |
| // three type of conversion in js: to string, to boolean, to number | |
| // conversion login for primitive and object work differently but both can only be converted to those three ways | |
| // String conversion: all primitives values are converted to string naturally => String(null) = 'null' | |
| // Symbol conversion is bit tricky, because it can only be converted explicitly, but not implicitly | |
| // Boolean Conversion: | |
| // Apply the Boolean() function to explicitly convert a value to boolean => Boolean(2) | |
| // Implicit conversion happen is locgical context or triggered by logical operators(|| && !) => if(2) / 2 || 'hello' | |
| // logical operators do boolean conversion internally but return the value of original operands | |
| // Numeric conversion: | |
| // explicit conversion - Number() | |
| // implicit conversion is tricky, because it triggered in more cases | |
| // 2 special rule: | |
| // 1 - when applying == to null or undefined numerci conversion does not happen. [null equals only to null or undefined] | |
| // 2 - NaN does not equal to anything even itself | |
| // ______________________________________________________________________________________ | |
| // Number and Maths: | |
| // numbers are stored as double floating point numbers [All numbers are float] | |
| // RegEx: is simply a type of object that is used to match character combinations in string. | |
| // two ways to construct a RegEx; | |
| // 1- Regular Expression Literal; | |
| var option = /cat/; // use literal for constant RegEx | |
| // 2 - RegEx constructor; | |
| var option2 = new RegExp("cat"); // if regex will be changing or relying on other variable then use constructor | |
| console.log("check regEx: " + /cat/.test("hello cat")); | |
| // Template literal [enclosing back tick] and Tag function[get argument]: | |
| var name = 'world'; | |
| console.log(`hello ${name}`); | |
| var p = { | |
| name: 'jackson', | |
| nn: 'jak' | |
| }; | |
| // String concatenation: | |
| console.log('Hi, I\'m ' + p.name + ' ! call me "' + p.nn +'". '); | |
| // Template literals: | |
| console.log(`Hi, I'm ${p.name} ! Call me "${p.nn}".`); | |
| var name1 = 'Foo'; | |
| var age = 23; | |
| function greet() { | |
| console.log(`I'm ${name1}. I'm ${age} year old`); | |
| } | |
| console.log(greet `I'm ${name}`); | |
| // String: Immutable | |
| let str1 = "abc"; | |
| console.log(str1); | |
| str1 = "def"; | |
| console.log(str1); | |
| const str2 = 'some cool sentence, yeah'; | |
| // string to array - use Strimg.split(separator, limit) => return an array of strings and split at each point where separator occurs. | |
| const word = str2.split(' '); | |
| console.log(word); | |
| // remove spaces | |
| // - String.trim() => username.trim(); / u2.trimStart() / u3.trimLeft() | |
| // check if string incluide a specific word | |
| // m1.includes('swag'); | |
| // with regex: RegExp(/swag/gi); re.test(m1); | |
| // replace part of string; str.replace(RegExp|Str, str|fun); | |
| // copy part of sentence: String.slice(start, end) => return a new string with extracted text | |
| // with Substring: String.Substring(start, end) | |
| // capitalize the first letter of a sentence: | |
| let sentence = "how are you"; | |
| const firstChar = sentence.slice(0, 1).toUpperCase(); // grab the first letter and uppercase it | |
| const otherChars = sentence.slice(1); | |
| console.log(sentence = firstChar + otherChars); | |
| // get the index of a word; use String.indexOf(search, start) and str.lastIndexOf() | |
| // pad a word | |
| let str3 = 'centred title'; | |
| const len = str3.length; | |
| str3 = str3.padStart(len + len / 2); | |
| str3.padEnd(len * 2); | |
| // startsWith, endsWith | |
| // count how many time phrase occurs | |
| const input = `I love coffee. | |
| I buy coffee everyday. | |
| I'm drinking coffee ... coffee right now.`; | |
| const wordCount = 'coffee'; | |
| // use string.match(RegExp|str) : g - find all matches : i - to ignore case | |
| function countWord(str, word) { | |
| const re = RegExp(word, 'gi'); | |
| const match = (str.match(re) || []); | |
| const count = match.length; | |
| return count; | |
| } | |
| console.log(countWord(input, wordCount)); | |
| // const a = new Array(3); | |
| // console.log(a.length); | |
| // console.log(typeof a); | |
| // const sum = new Function('a', 'b', 'return a + b'); | |
| // console.log(sum(2, 3)); | |
| // const str = '24.33'; | |
| // const short = +str; | |
| // const num = Number(str); | |
| // console.log(num); | |
| // const bin = '110110' | |
| // const b = Number.parseInt(bin, 2); | |
| // console.log(b); | |
| // console.log(Number.isInteger(5/2)); | |
| // const a1 = 5.27158; | |
| // console.log(a1.toFixed(2)); | |
| // console.log((23).toString(2)); | |
| // console.log(5 + NaN); | |
| // // genertate random number between min to max including min and max; | |
| // const getRandom = (min, max) => { | |
| // Math.floor(Math.random() * (max - min + 1)) + min; | |
| // } | |
| // console.log(getRandom(1, 7)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment