Working with Strings in JavaScript

Working with Strings in JavaScript

Javascript has many types of data types. String is one of them. It is one of the most essential data types in JS. In this article we will walk through the string data type, declaring and inbuilt methods to work with strings in JS.

Now the question arises what actually is a String ?

In computer programming , a string is a sequence of characters used to represent a text.

String Data types are used to store these characters. These strings are enclosed inside “ “ or ‘ ‘. Both are valid ways to define a string.

There is also another way to represent a string and this is by backticks. They are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with ${variable or expressions} as shown below.

//string example

const greet = ‘Hello’
const name = “Saurav Purohit”;
const introduction = `${greet}, my name is ${name}`;

Multiline Strings

To use a multiline string, you can either use the + operator or the \ operator.

For example,

// using the + operator
const message1 = 'This is a long message ' +
    'that spans across multiple lines' + 
    'in the code.'

// using the \ operator
const message2 = 'This is a long message \
that spans across multiple lines \
in the code.'

FInding the String Length

To find the length of a string, you can use built-in length property.

For example,

const a = 'hello';
console.log(a.length); // 5

String Methods

  • Converting a Unicode number into a character
String.fromCharCode(72, 101, 108, 108,111);
// Output: Hello
  • Returning the character from a specified index
'Laptop'.charAt(3);
// Output: d
  • Concatenating two or more strings
'Hello'.concat(' World');
// Output: Hello World
  • Checking whether a string ends with a specified string
'Hi there!'.endsWith('!');
// Output: true
  • Checking whether a string contain a given characters
'Hi, My name is Saurav'.includes('name');
// Output: true
  • Finding the first occurrence of a specified value in a String
'JavaScript'.indexOf('S')
// Output: 4
  • Comparing two strings in the current locale
'a'.localeCompare('b');
// Output: -1
  • Searching a string for a match
'abcABCdefDEF'.match(/[A_C]/g);
// Output: ['A', 'B', 'C']
  • Padding a given string with another string on the right side until a given length
'Hi'.padEnd(6, '!');
// Output: Hi!!!!
  • Padding a given string with another string on the left side until a given length
'Hi'.padStart(6, '!');
// Output: !!!!Hi
  • Returning a new string with a specified number of copies.
'<3'.repeat(4);
// Output: <3 <3 <3 <3
  • Replacing a string with another string
'Like JavaScript'.replace(/Like/, 'Love');
// Output: Love JavaScript
  • Searching a string for a specified value and returns the position of the match
'JavaScript Python Ruby'.search(/Python/);
// Output: 11
  • Selecting a specific part of an array
'get me this part'.slice(7, 11);
// Output: this
  • Splitting a string into an array of substrings
'split these words'.split(' ');
// Output: ["split", "these", "words"]
  • Checking whether a string begin with a specified string
'How are you?'.startsWith('How')
// Output: true
  • Converting a string into lowercase letters
'MAKE ME SMALL'.toLowerCase();
// Output: make me small
  • Converting a string into uppercase letters
'make me big'.toUpperCase();
// Output: MAKE ME BIG
  • Removing whitespace from both ends of a string
'   extra spaces    '.trim();
// Output: 'extra spaces'
  • Removing whitespace from right end of a string
'   extra spaces    '.trimEnd();
// Output: '    extra spaces'
  • Removing whitespace from left end of a string
'   extra spaces    '.trimStart();
// Output: 'extra spaces    '
  • Performing concatenation that result in the same template literals
String.raw`no \n enters \n today`;
// Output: no \\n enters \\n today