Types and String Methods Flashcards

(14 cards)

1
Q

The 5 data types

A

string - for holding text (for now, put text in backticks (`))
number - for holding numbers
boolean - for holding a true or false token
null - a placeholder for representing a value which is not currently valid but we expect to be populated later
undefined - EVERYTHING starts off as undefined. If you declare a variable and give it no value then its value is ‘undefined’
undefined is both a type and an system created object

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

How to test an enitity for its type?

A

typeof <someThing></someThing>

returns a string representation of the type:

string: ‘string’
number: ‘number’
boolean: ‘boolean’
null: ‘object’
undefined: ‘undefined’
object: ‘object’
array: ‘object’
function: ‘function’

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

How to change type by CASTING e.g. Number(‘22’)

A

JavaScript is ‘loosely typed’; which means that you can change the type of a something once it has been created:

// Casting
// Use of number constructor to change type
typeof ‘11’; // string
typeof Number(‘11’); // number

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

How to change type by COERCION e.g. ‘22’ * 1

A

// Type Coercion
// Multiplying the string causes its coercion to a number
typeof ‘11’*1; // number

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

How to find how many letters are in the string and how to make excess for changing it?

A

const myString = “James”;

myString[0] // to get the first letter of the string
myString[2] // to get the third letter of the string

// Accessing beyond the length causes a RangeError
myString[96] // undefined
myString[-2] // undefined

// Accessing in reverse order
myString[myString.length - 1]; // last letter
myArray[myArray.length - 2]; // penultimate item

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

toUpperCase(), toLowerCase()

A

const sentence = ‘The quick brown fox jumps over the lazy dog.’;

console.log(sentence.toUpperCase());

// Expected output: “THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.”

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

slice()

A

The slice() method of String values extracts a section of this string and returns it as a new string, without modifying the original string.

const str = ‘The quick brown fox jumps over the lazy dog.’;

console.log(str.slice(31));
// Expected output: “the lazy dog.”

console.log(str.slice(4, 19));
// Expected output: “quick brown fox”

console.log(str.slice(-4));
// Expected output: “dog.”

console.log(str.slice(-9, -5));
// Expected output: “lazy”

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

concat()

A

The concat() method of String values concatenates the string arguments to this string and returns a new string.

const str1 = ‘Hello’;
const str2 = ‘World’;

console.log(str1.concat(‘ ‘, str2));
// Expected output: “Hello World”

console.log(str2.concat(‘, ‘, str1));
// Expected output: “World, Hello”

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

search()

A

The search() method of String values executes a search for a match between a regular expression and this string, returning the index of the first match in the string.

const paragraph = ‘The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?’;

// Any character that is not a word character or whitespace
const regex = /[^\w\s]/g;

console.log(paragraph.search(regex));
// Expected output: 43

console.log(paragraph[paragraph.search(regex)]);
// Expected output: “.”

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

replace()

A

const p = ‘The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?’;

console.log(p.replace(‘dog’, ‘monkey’));
// Expected output: “The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?”

const regex = /Dog/i;
console.log(p.replace(regex, ‘ferret’));
// Expected output: “The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?”

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

matchAll()

A

const regexp = /t(e)(st(\d?))/g;
const str = ‘test1test2’;

const array = […str.matchAll(regexp)];

console.log(array[0]);
// Expected output: Array [“test1”, “e”, “st1”, “1”]

console.log(array[1]);
// Expected output: Array [“test2”, “e”, “st2”, “2”]

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

valueOf()

A

const stringObj = new String(‘foo’);

console.log(stringObj);
// Expected output: String { “foo” }

console.log(stringObj.valueOf());
// Expected output: “foo”

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

trim()

A

The trim() method of String values removes whitespace from both ends of this string and returns a new string, without modifying the original string.

const greeting = ‘ Hello world! ‘;

console.log(greeting);
// Expected output: “ Hello world! “;

console.log(greeting.trim());
// Expected output: “Hello world!”;

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

repeat()

A

The repeat() method of String values constructs and returns a new string which contains the specified number of copies of this string, concatenated together.

const mood = ‘Happy! ‘;

console.log(I feel ${mood.repeat(3)});
// Expected output: “I feel Happy! Happy! Happy! “

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