Warning: session_start(): open(/tmp/sess_28e40ac434b98845868a5ebcf5014c39, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Prepend A String To The Beginning Of Another String In JavaScript - LearnShareIT

How To Prepend A String To The Beginning Of Another String In JavaScript

Prepend a String to the Beginning of Another String in JavaScript

During JavaScript web application development, we may get a requirement to prepend a string to the beginning of another string. Read this article’s end to prepend a string to the beginning of another string in JavaScript.

Prepend a string to the beginning of another string in JavaScript

We have the following four solutions:

  • Use the Template Literals 
  • Use the Addition Operator
  • Use the replace() method
  • Use the concat() method

Now, we will take a closer look at each solution with specific examples for each of them.

Use the Template Literals 

Pattern characters are contained in backtick character pairs (`).

Template literals are commonly used for string interpolation (create strings by doing substitution of placeholders).

Syntax:

${...}

Example:

const lsi1 = 'IT';

// Prepend the string 'LearnShare' to the beginning of the string 'IT'
const lsi2 = `LearnShare${lsi1}`;

console.log(lsi2);

Output:

LearnShareIT

Click here if you want to learn more about template literals.

Use the Addition Operator

The addition operator (+) sums the operands if they are numbers or concatenates them if at least one operand is a string.

Example:

const lsi1 = 'IT';

// Prepend the string 'LearnShare' to the beginning of the string 'IT'
const lsi2 = 'LearnShare' + lsi1;

console.log(lsi2);

Output:

LearnShareIT

Use the replace() method

The replace() method returns a new string where the specified value(s) has been replaced.

Syntax:

string.replace(searchValue, newValue)

Parameters:

  • searchValue: The value, or regular expression, to search for.
  • newValue: The new value to replace.

We set the searchValue parameter to an empty string in the following example. Therefore, the replacement string is added to the beginning of the original string.

Example:

const lsi1 = 'IT';

// Prepend the string 'LearnShare' to the beginning of the string 'IT'
const lsi2 = lsi1.replace ('','LearnShare');

console.log(lsi2);

Output:

LearnShareIT

Use the concat() method

The concat() method returns a new string containing the concatenated strings.

Syntax:

string.concat(string1, string2, ..., stringX)

Parameters:

  • string1, string2, …, stringX: Strings concatenated.

Example:

const lsi1 = 'IT';

// Prepend the string 'LearnShare' to the beginning of the string 'IT'
const lsi2 = "LearnShare".concat(lsi1);

console.log(lsi2);

Output:

LearnShareIT

Summary

This article has shown how to prepend a string to the beginning of another string in JavaScript. I hope the information in this article will be helpful to you. If you have any problems, please comment below. I will answer. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *