How To Work with Strings in JavaScript?

How To Work with Strings in JavaScript?

In JavaScript, a string is a sequence of characters typically used to represent text. Strings are one of the primitive data types in JavaScript, along with numbers, booleans, nulls, and undefineds. Strings can be created using single or double quotes, and a String object provides methods for working with strings.

Here are some examples of creating strings in JavaScript:


let firstStr = 'Hello, World!';
let secondStr = "Hello, World!";

Both single and double quotes are valid for creating strings, and you can use them interchangeably. Additionally, you can create strings using the `String`  constructor:

let str = new String(‘Hello, World!’);

Once you have a string, you can perform various operations, such as concatenation, accessing individual characters, finding the length, and using multiple string methods for manipulation.

  • String Concatenation
  • String Length
  • Accessing Characters
  • Substring
  • Template Literals
  • String Methods
  • String Conversion:

String Concatenation:

You can concatenate strings using the + operator or `concat` methods. Both approaches achieve the same result: combining multiple strings into a single string.

Example:


>let str1 = 'Hello';
let str2 = 'World';
let result = str1 + ' ' + str2;
console.log(result);
let result2 = str1.concat(', ', str2, '!');
console.log(result2)

OUTPUT


Hello World
Hello, World! 

String Length:

You can find the length of a string using the length property:

Example:


let str = 'Hello, World!';
console.log(str.length);

OUTPUT

13
  • Accessing Characters: You can access individual characters in a string using square brackets and zero-based indexing:

Example:


let str = 'Hello, World!';
console.log(str[0]);

OUTPUT

H

Try our Java Hosting services with $100 free credits!

Substring:

Substring method to extract a portion of a string:

Example:


let str = 'Hello, World!';
let substr = str.substring(0, 5);
console.log(substr);

OUTPUT

Hello

Template Literals:

Template literals provide a more convenient way to work with strings, allowing you to embed expressions:


let helloStr = "Hello"
let finalStr = `${helloStr} Words!`
console.log(finalStr)

OUTPUT:

Hello Words! 

Template literals are enclosed by backticks (`) instead of single or double quotes.

Check our developer-friendly Java Hosting services.

String Methods:

JavaScript provides a variety of string methods for manipulation. Some common ones include:

toUpperCase() and toLowerCase(): Converts a string to uppercase or lowercase.

indexOf(): Returns the index of the first occurrence of a substring.

replace(): Replaces a specified substring or pattern with another string.

trim(): Removes whitespace from both ends of a string.

Example:


let str = '   Hello, World!   ';
console.log(str.toUpperCase());
let str = '   Hello, World!   ';
console.log(str.trim());
let str = 'Hello, World!';
console.log(str.indexOf('!'));
let str = 'Hello, World!';
console.log(str.replace('Hello', 'Hi'));

OUTPUT:

HELLO, WORLD!
Hello, World!
12
Hi, World! 

Get Containerized Java Hosting with Auto Scalability!

String Conversion:

You can convert values to strings using the `toString()` method or the `String()` constructor:

Example:

let num = 123;
let strNum = num.toString();
console.log(typeof strNum);

OUTPUT:

string

Conclusion

In JavaScript, manipulate strings by creating, concatenating, and accessing characters. Use methods for tasks like substring extraction and case conversion. Template literals simplify string creation with embedded expressions.