JavaScript: Strings, String Methods, and String Properties



This content originally appeared on DEV Community and was authored by Megan Paffrath

Strings

Strings are primitive values in JS that contain letters and characters. Some examples of such include:

let firstName = 'Megan';
let lastName = "Paffrath";
let quotation = '"Man is asked to make of himself what he is supposed to become to fulfill his destiny" -Paul Tillich';
let sentence = "Let's code some fun stuff";

String Properties

String properties are the properties applied to a string, such as string length as shown below:

let whatUp = 'What Up!';
console.log(whatUp.length); // 8

String Methods

String methods are methods that we call on a string that perform an action. Some examples include:

let intro = " Hello there! ";
console.log(intro.toUpperCase()); // HELLO THERE! (note the space before and after)
console.log(intro.trim()); //HELLO THERE!(note the removal of spaces before and after)

String Methods with Arguments

These are methods that accept arguments, as shown bellow:

let whatUp = "What Up Tho!";
console.log(whatUp.indexOf('Up')); // 5
console.log(whatUp.indexOf('$')); // -1
console.log(whatUp.slice(5)); // Up Tho!
console.log(whatUp.slice(1,4)); // hat
console.log(whatUp.replace('Tho', 'Friend?')); // What Up Friend?
console.log('lol'.repeat(10)); // lollollollollollollollollollol

note: these are some notes for myself from Section 15 of The Web Developer Bootcamp. To learn more, check out the course.


This content originally appeared on DEV Community and was authored by Megan Paffrath