Types and String Methods Flashcards
(14 cards)
The 5 data types
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 to test an enitity for its type?
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 to change type by CASTING e.g. Number(‘22’)
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 to change type by COERCION e.g. ‘22’ * 1
// Type Coercion
// Multiplying the string causes its coercion to a number
typeof ‘11’*1; // number
How to find how many letters are in the string and how to make excess for changing it?
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
toUpperCase(), toLowerCase()
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.”
slice()
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”
concat()
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”
search()
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: “.”
replace()
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?”
matchAll()
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”]
valueOf()
const stringObj = new String(‘foo’);
console.log(stringObj);
// Expected output: String { “foo” }
console.log(stringObj.valueOf());
// Expected output: “foo”
trim()
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!”;
repeat()
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! “