JavaScript String

JavaScript String

·

3 min read

A String is a sequence of characters enclosed with double or single quotes. Strings are used to hold data that are represented in text form. They have several methods and properties used to operate on them which we will discuss in this article.

Methods and Operations.

Creating string.

You can create a new string using the String constructor

const myName = new String("John Doe")

The problem with this method is that if you check it’s type, you’ll notice it return “object” instead of “string”. This is said to be an error in the JavaScript lexeme, so you might want to avoid using the constructor to create string. Rather, use the String function without the new keyword.

const name = new String("John Doe");
console.log(typeof name); // object

const name = String("John Doe");
console.log(typeof name); // string

But anyways, a better, easier and faster way to create string is by using string literals.

const firstName = "John";
const lastName = 'Doe';
const favoriteColor = `Black`;

Get character

To get a single character from a string, you can use the .charAt() function or square brackets and an index like you’re trying to access an element of an array.

const name = "John Doe";
console.log(name.charAt(2)); // h
console.log(name[2]); // h

Get set of characters.

To get more than a single character, you’d need to specify a range to the subString() function.

const sentence = "The quick brown fox jumped over the lazy dog.";
console.log(sentence.substring(4, 19)); // quick brown fox

String comparison.

To check equality or inequality in strings, you can use the traditional comparison operators for numeric values. Works like magic.

let alpha1 = "a";
let alpha2 = "b";
console.log(alpha1 < alpha2); // true

let alpha3 = "c";
let alpha4 = "d";
console.log(alpha3 > alpha4); // false

let alpha5 = "a";
let alpha6 = "a";
console.log(alpha5 === alpha6); // true

let firstName = "John";
let lastName = "John";
console.log(firstName === lastName); // true

String casting.

Casting in computer science means changing the data type of a piece of data to another. To change the data type of a primitive value to string, you’ll need to use either one of three methods.

  1. .toString() method.

    let n = 40;
    console.log(typeof n); // number
    n = n.toString();
    console.log(typeof n); // string
    
  2. String() Function

    let n = 40;
    console.log(typeof n); // number
    n = String(n);
    console.log(typeof n); // string
    
  3. Template literal

    let n = 40;
    console.log(typeof n); // number
    n = `${n}`;
    console.log(typeof n); // string
    
  4. String coercion

    let n = 40;
    console.log(typeof n); // number
    n = "" + n;
    console.log(typeof n); // string
    

You can check the position of a set of characters in your string with the .indexOf() method.

const sentence = "The quick brown fox jumped over the lazy dog.";
console.log(sentence.indexOf("fox")); // 16

This is typically used to check the existence of a set of characters in a string body.

String decomposition and composition

You can use the .split() function to decompose a string into an array and .join() function to compose a string from an array.

let sentence = "The quick brown fox jumped over the lazy dog.";
sentence = sentence.split(" ");
console.log(sentence); // ['The','quick','brown','fox','jumped','over','the','lazy','dog.']
console.log(sentence.join("_")); // "The_quick_brown_fox_jumped_over_the_lazy_dog."

String replace

You can take a string and look for an occurrence of a particular substring and replace it with something else with the .replace() function.

let sentence = "The quick brown fox jumped over the lazy dog.";
console.log(sentence.replace("fox", "ferret")); // The quick brown ferret jumped over the lazy dog.

That brings us to the end of our article of JavaScript Strings. Thanks for reading.