TIL: Today I Learned
[TIL] 20201126 JavaScript Strings and More
김알리
2020. 11. 27. 14:33
String
- Represents text
- Must be wrapped in quotes, either double or single.
- Warning : 23 ≠ '23'
let username = "Jiwan";
let username = 'Jiwan';
let username = 'Jiwan"; //error
String Index
- Strings are indexed. Each character has a corresponding index (positional number)
- Index starts at 0.
String Length
- Length of index.
- (Index of last letter) + 1 = Length
String Concatenation
- Put multiple strings or non-string types together.
- "String1" + "String2" = "String1String2"
- 1 + "String" = "1String"
String Method
- Built-in actions we can perform with individual strings.
- String methods are used when
- Searching within a string
- Replacing part of a string
- Changing the casing of a string
- Syntax
- something.method();
- examples
- toUpperCase()
- toLowerCase()
- trim()
String Methods with Arguments
- Argument
- Inputs that some methods accept inside of parentheses.
- Syntax : something.method(arg)
- examples
- indexOf()
'catdog'.indexOf('cat'); //0
'catdog'.indexOf('z'); //-1 (not found)
String Template Literals
- Strings that allow embedded expressions, which will be evaluated and then turned into a resulting strings.
- Back-ticks( ` ) are used, not single quotes.
- Syntax : `String ${expression} String`
Undefined & Null
- Null
- Intentional absence of any value.
- Must be assigned.
- Undefined
- Variables that do not have an assigned value are undefined.
Random Numbers & The Math Object
- Math Object
- Contains properties and methods for mathematical constants and functions.
- Methods
- Math.round() : Rounding a number.
- Math.abs() : Absolute value
- Math.pow(x,y) : Raises x to the yth power
- Math.floor() : Removes decimal
Math.round(4.9); //5
Math.abs(-3); //3
Math.pow(2,5); //32
Math.fllor(3.8); //3
- Random Number
- Gives a random decimal between 0 and 1 (non-inclusive: doesn't include 1)
- Math.random();
* This post is a summary of Udemy Course "The Web Developer Bootcamp" by Colt Steele.