Strings:
Slice()
1. What does it manipulate(is it pure)?
2. Inputs?
3. Outputs?
syntax
slice(start, end)
const str = '12 4567 910'; console.log(str.slice(0, 4)); // Expected output: "12 4"
String
length
Its a property
exists on an instance of a string class
let text = "ABCD"; let len = text.length console.log(len) // 4
String
includes()
Method
It is a case-sensitive method.
string.includes(searchvalue, start)
**NOTE: ** it will treat the Uppercase characters and Lowercase characters differently.
String
replace()
Method
pattern (string), replacement (string or a function) called for each matchreplace(pattern, replacement)
the pattern can can also be a RegEx expression
string
subString()
Method
is similar to slice()
indexStart up to but not including indexEnd.const str = 'Gozilla'; console.log(str.substring(1, 3)); // Expected output: "oz"
substring(start, end)
begins at a specified position, and returns a specified number of characters.
Strings
Bracket Notation
Special Syntax
Allows to access the individual characters that make up a string.
let text = "HELLO WORLD"; let char = text[0]; // H
syntax
String
How to loop over a string?
Iterator Method
const str = "abcdefu"'
for (let i = 0; i < str.length; i++) {
console.log(str[i]);
} // "a" "b" "c" "d" "e" "f" "u"Strings
concat()
const str = "cuoewcbeiyx" console.log(str.concat(9)); console.log(str); // cuoewcbeiyx9 // cuoewcbeiyx
Strings
template literals
Template Literals
string Interpolation
const firstName = "Shams"
const contex = " is challenging me"
console.log(`${firstName} ${context}`);
// expects: "Shams is challenging me"synonyms
String
\+
Es5: “+”
const str1 = "wuuaao"; const str2 = "....."; console.log(str1 + str2);
+ operaters works like concat() for strings
Strings
toUpperCase()
changes case sensitive string to Upper Case
const str = "i want to love you"; console.log(str.toUpperCase());
String
toLowerCase()
changes upper case charaters into case sensitive
const str = "I WANT TO GET THIS DONE Already!" console.log(str.toLowerCase()); // expected: i want to get this done already!
String
split()
Method
splits a string and returns a new array as substrings
* seperator and limit (if no parameter is placed it returns the string in the array)
const str = "I WANT TO GET THIS DONE Already!" console.log(str.toLowerCase().split()); // [ 'i want to get this done already!' ]
const str = "I WANT TO GET THIS DONE Already!"
console.log(str.toLowerCase().split(" ", 5));
// [ 'i', 'want', 'to', 'get', 'this' ]Arrays
forEach()
method
const myBlessing = ["Shams", "is very lucky", "and so am I"];
//I: a function and string values
myBlessing.forEach((message) => {
console.log(message);
}); //O: logs the Strings in the array
Shams
is very lucky
and so am IArrays
map()
callback function, @data type value, @index, and, @arraycreates a new array with the results of calling a provided function on e
Arrays
filter()
method
array.filter(function(currentValue, index, arr), thisValue)
Arrays
concat()
Arrays
Array.from()
static method
creates a new, shallow-copied Array instance from an iterable or array-like object.
_.defaults(object, [sources])
Arguments:
* object (Object): The destination object.
* [sources] (…Object): The source objects.
Returns:
* (Object): Returns object.
Impure
mutates object.