javascript Tutorial

JavaScript String

Learn more about one of the world’s most popular programming languages.

A JavaScript string is a data type for storing textual information. JavaScript strings can 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

Code Example
const greeting = ‘Merry Christmas’;

const holidayGreeting = `Wishing you all a joyous ${greeting}`;

// prints ‘Wishing you all a joyous Merry Christmas’

JavaScript String Properties and Methods

Although normally a primitive value would not have properties or methods because they are not objects, JavaScript is an exception because primitive values are wrapped by objects providing them with properties and methods.

JavaScript String Methods

JavaScript provides a lot of helper or built-in string 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 JavaScript strings are:

  • String.prototype.includes(searchString [, position]) — Determines whether the calling string contains searchString
  • String.prototype.slice(beginIndex[, endIndex]) — Extracts a section of a string and returns a new string
  • String.prototype.split([sep [, limit] ]) — Returns an array of strings populated by splitting the calling string at occurrences of the substring sep
  • String.prototype.localeCompare(compareString [, locales [, options]]) — Returns a number indicating whether the reference string compareString comes after, before, or is equivalent to the specified string in sort order
  • String.prototype.normalize([form]) — Returns the Unicode Normalization Form of the calling string value
  • (index) — Returns the character at the specified index

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 Web 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.

Code Example
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.

Code Example
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.

Code Example
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.

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

Creating JavaScript Strings

A new string can be created using the String() constructor. New strings can be created as primitives or string objects. In most situations, you can use a string primitive or a string object interchangeably.

JavaScript String Length

Use the built-in length property to find the length of a string.

Learn JavaScript Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required