String Flashcards

1
Q

Attributes

A
Primitive data type / immutable
Contains properties & methods
#safe (ES1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Instantiation

A
// literal
"literal"
// constructor; arg is converted into string
String("literal")
new String("object")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Properties

A
.length
=>  {num} length of string; not exact if contains uncommon chars
#safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Methods (16)

A

String.fromCharCode — convert char codes into string representation

.charCodeAt — numeric Unicode value @ index

.indexOf — index of first occurring substring

.lastIndexOf — index of last occurring substring

.match — array of matching substring/pattern

.replace —string w/ replacements

.search —first occurring index of pattern

.slice —substring based on start & end index; indexes can be relative to beginning or ending of string

.split —break up string into an array of strings

.substr —substring based on start index & length

.substring —substring based on start & end index

.toLowerCase —string lowercased

.toUpperCase —string uppercased

.trim —string w/ front & end whitespace stripped

.toString —same as .valueOf

.valueOf —return string in primitive form

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Comparing strings with > type operators. How is comparing done? What about uppercase?

A

Comparisons are done lexically, so “d” is greater than “c”. Uppercase is only less than lowercase if the strings are otherwise equal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Concatenating and comparing strings against non strings.

A

Concatenating a non string will first convert the value into a string. Comparing non strings against strings using the non strict equality operator (==) will also convert the non string value to a string first.

”” + undefined // => “undefined”
“” + null // => “null”
“” + true // => “true”
“” + 3.1 // => “3.1”
“” + function () { return 1; } // => “function () { return 1; }”
“” + {} // => “[object Object]”
“” + [1, 2] // => “1,2” - Arrays apply .join() w/ default arg of “,”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Extract a single character from a string by index (2)

A

“string”[1] // => “t”

Treat the string as an array-like object, where individual characters correspond to a numerical index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly