Strings, Arrays, and Object Methods Flashcards
Strings:
Slice()
1. What does it manipulate(is it pure)?
2. Inputs?
3. Outputs?
syntax
- Does not modify the original string. pure
- method takes 2 parameters: start position, and end position (end not included).
- Extracts a section of a string and returns a new string. creates a new string
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
- Doe not manipulate
- property returns the length of a string
let text = "ABCD"; let len = text.length console.log(len) // 4
String
includes()
- What does it manipulate(is it pure)?
- Inputs?
- Outputs?
Method
It is a case-sensitive method.
- Pure
- searchValue, start (defaults to 0)
the position within the string at which to begin searching for (optional) - Boolean value
string.includes(searchvalue, start)
**NOTE: ** it will treat the Uppercase characters and Lowercase characters differently.
String
replace()
- What does it manipulate(is it pure)?
- Inputs?
- Outputs?
Method
- does not mutate the original string. pure
method replaces a specified value with another value in a string. -
pattern
(string),replacement
(string or a function) called for each match - method returns a new string with one, some, or all matches of a pattern replaced by a replacement
replace(pattern, replacement)
the pattern can can also be a RegEx expression
string
subString()
- What does it manipulate(is it pure)?
- Inputs?
- Outputs?
Method
is similar to slice()
- does not mutate the string. pure
- extracts characters from
indexStart
up to but not includingindexEnd
. - A new string containing the specified part of the given string.
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
- What does it manipulate(is it pure)?
- Inputs?
- Outputs?
Special Syntax
Allows to access the individual characters that make up a string.
- Does Nothing. property accesor, can extract string characters as read only by index
- If no character is found, [ ] returns undefined.
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()
- combines two or more arguments and returns a new srting
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
- Template Literals
- Template Strings
- String Templates
- Back-Tics Syntax
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
- Iterates through arrays
- takes any value and a function to manipulate the value
- returns nothing, just iterates
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 I
Arrays
map()
- pure
- Iterates through an array and returns a new array
-
callback function
,@data type value
,@index
, and,@array
creates a new array with the results of calling a provided function on e
Arrays
filter()
method
- pure, does not change the original array
- callback function, this value
- returns a new array filled with the elements that pass a test provided by a function
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.