DEV Community

Megan Paffrath
Megan Paffrath

Posted on • Edited on

JavaScript: Strings, String Methods, and String Properties

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";
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay