7554425D-0054-440D-B95E-D2ABC13D62CD Created with sketchtool.
Your Saved Info Packages

View your saved Course or Program Packages containing pricing and detailed curriculum.

Speak with a Learning Advisor.

Have any questions? We'll call you.

Fill out the form below and a Learning Advisor will reach out at a time convenient for you.

Please pick a valid date and time between 9 AM and 8 PM eastern (Monday to Friday)

By clicking "Book a call," you accept our Terms and will also receive exclusive offers and updates about new courses, workshops and events.

JavaScript Tutorial

JavaScript Strings

Ready to start your career in Development? Find out more about BrainStation's Web Development Bootcamp

Strings is a data type in JavaScript or any other programming language to store textual information. It could be a word or a sentence or an entire block of text. String is a built-in primitive data type and can be used easily by putting the text in a single-quote , double-quote or back-ticks ```. For example,

  • ‘Merry Christmas’
  • “Merry Christmas”
  • Merry Christmas

The last variation using back-tick is used for special purposes while storing or working with textual information. It allows adding values to a text dynamically by putting them in between ${} inside of a back-tick string. Let’s look at an example

const greeting = ‘Merry Christmas’;

const holidayGreeting = `Wishing you all a joyous ${greeting}`;
// prints ‘Wishing you all a joyous Merry Christmas’

JavaScript built-in methods

JavaScript provides a lot of helper or built-in methods on String data types that are useful to do a certain type of operations without having to write the logic for it. Some of the most common methods used with strings are: String.prototype.includes(searchString [, position]) String.prototype.slice(beginIndex[, endIndex]) String.prototype.split([sep [, limit] ]) String.prototype.replace(searchFor, replaceWith)

There are quite a few useful methods apart from this which can be found on official MDN Documentation for JavaScript string data type.

JavaScript string includes or contains another string

JavaScript provides an includes method to do a case sensitive search for a given text within a string. It returns true if the string is found, else it returns false. A Developer can also specify at which position the search should start and if not specified it defaults to 0 which is the start of the string.

const greeting = ‘Wishing you a happy birthday’;

console.log(greeting.includes(‘happy’)); // returns true
console.log(greeting.includes(‘Happy’)); // returns false
console.log(greeting.includes(‘happy’, 14)); // returns true
console.log(greeting.includes(‘happy’, 15)); // returns false

JavaScript string replace

replace() is a method that provides a way to replace a part of a string with another string.

let str = 'the night before Xmas...';
let newstr = str.replace('Xmas', 'Christmas');
console.log(newstr);  // the night before Christmas…

JavaScript split string

split() method splits a given string on the basis of a separator. It returns an array of strings from a given string after splitting them based on the separator.

let namesString = ‘john,dave,mike,alex’;
let namesArray = namesString.split(‘,’); // split on comma

console.log(namesArray); // [‘john’, ‘dave’, ‘mike’, ‘alex’]

JavaScript string to number

Sometimes there is a need to convert a string into a number. This can be easily done by using the Number constructor function Number() and if the string that is being converted into number holds numerical value in it, the constructor function should return the value as a number.

const grades = ‘100’;
const gradesNum = Number(grades);
console.log(gradesNum); // prints 100;
console.log(typeof gradesNum); // prints number;
Set Password

You already have an account with BrainStation, but you still need to set up a password.