Method Chaining
You can attach tools back-to-back. word.trim().toUpperCase() first cleans the spaces, then immediately capitalizes the result in one smooth motion.
Immutability of Strings
Once a string is created, the original cannot be altered. Methods like toUpperCase() don't change the original word; they spit out a brand new copy of it.
String Methods: endsWith()
Checks if the string ends with specific characters. Great for checking if a file upload ends in '.jpg' or '.png'.
String Methods: startsWith()
Checks if the string begins with specific characters. Useful for checking if a URL starts with 'https://'.
String Methods: includes()
Returns a simple true or false. sentence.includes('alien') checks if the word 'alien' exists anywhere inside the sentence.
String Methods: trim()
Users accidentally add spaces when typing. trim() shaves off invisible spaces from the far left and far right of a string, cleaning up the data.
String Methods: split()
A powerful tool that chops a string into pieces and turns it into an Array list. Splitting a sentence by spaces creates an array of individual words.
String Methods: replaceAll()
Regular replace() only targets the very first match it finds. replaceAll() hunts down every single instance in the entire paragraph and changes them all.
String Methods: replace()
Finds a specific word inside a sentence and replaces it. sentence.replace('bad', 'good') swaps the first instance of 'bad' with 'good'.
String Methods: substring()
Almost identical to slice, but it refuses to accept negative numbers. If you give it a negative, it treats it as a zero.
slice() with Negatives
If you give slice a negative number, like slice(-2), it counts backward from the end of the word, extracting the last two letters.
String Methods: slice()
Acts like a pair of scissors. word.slice(0, 3) cuts out the letters starting at position 0 and ending at position 3. It pulls out a sub-section.
String Methods: indexOf()
Acts like a search engine for your word. word.indexOf('e') tells you exactly which numerical position the letter 'e' is located at within the string.
String Methods: toLowerCase()
The exact opposite. Converts all characters to lowercase. A must-have for checking email addresses in a database.
String Methods: toUpperCase()
Instantly converts the entire string to capital letters. Useful for normalizing user input so 'yes', 'Yes', and 'YES' all look identical to the computer.
Accessing Characters
You can pluck a single letter out of a word using brackets. If word = 'Code', word[0] gives you 'C'.
Index Positions
Computers start counting at zero. In the word 'APPLE', 'A' is at position 0, 'P' is at position 1. This zero-based counting is a core rule of programming.
The Length Property
Typing 'password.length' tells you exactly how many characters are in the string. If the user typed '1234', the length is 4.
Deep Dive: Strings
We know strings are text, but JS treats strings like an Array of characters. The word 'Cat' is actually treated as three separate letters: C, a, t.
Math.PI
Need the exact value of Pi for circle calculations? You don't need to type 3.14159... Just type Math.PI and the computer provides the exact value.
Math.sqrt()
Calculates the square root of a number. Math.sqrt(25) returns 5. Highly useful for calculating distances in video games or mapping software.
Math.pow()
The older way to do exponents before '**' existed. Math.pow(2, 3) means 2 to the power of 3.
Math.max() & Math.min()
Give it a list of numbers, and it finds the largest (max) or smallest (min). Math.max(10, 50, 2) instantly spits out 50.
Creating a Dice Roll
To get a number from 1 to 6, you combine tools: Math.floor(Math.random() * 6) + 1. It scales the random decimal up, chops off the tail, and shifts it.
Math.random()
Generates a random decimal between 0.0000 and 0.9999. It is the core engine for dice rolls, enemy spawns, and random events in coding.
Math.trunc()
The butcher. It completely chops off the decimal and leaves the whole number, ignoring rounding rules entirely. 4.99 becomes 4.
Math.ceil()
The ceiling function. It always pushes a decimal up to the next whole number. 4.1 becomes 5. Useful when you need whole buckets to hold items.
Math.floor()
The gravity function. It always pulls a decimal down to the nearest whole number. 4.9 becomes 4. It never rounds up.
Math.round()
Rounds a number to the nearest integer, just like in school. 4.4 becomes 4. 4.6 becomes 5.
The Math Object
JS comes with a built-in calculator called the 'Math' object. It contains pre-written tools to handle complex numbers so you don't have to invent the formulas.
Lecture 04: Operators, Maths and Strings
Total Duration: 1 hrs 55 mins 27 secs