JavaScript Strings
Strings are one of the most fundamental data types in JavaScript. They're used to
represent and manipulate text. In this guide, we'll explore various aspects of
JavaScript strings, from creation to manipulation and conversion.
Creating Strings
In JavaScript, you can create strings in two main ways:
1. Using String Literals
This is the most common and straightforward way to create strings:
const string1 = "manoj kumar";
2. Using the String() Constructor
While less common, you can also create strings using the String() constructor:
const string2 = new String("manoj nishad");
It's important to note the difference between these two methods:
console.log(typeof string1); // string
console.log(typeof string2); // object
The String() constructor creates a String object, not a primitive string value.
String Properties and Methods
Length
JavaScript Strings 1
You can get the length of a string using the length property:
console.log(string1.length); // 11
console.log(string2.length); // 12
Accessing Characters
You can access individual characters in a string using either the charAt() method
or square bracket notation:
console.log(string1.charAt(1)); // a
console.log(string2[11]); // d
Getting the String Value
For String objects, you can use the valueOf() method to get the primitive string
value:
console.log(string1.valueOf()); // manoj kumar
console.log(string2.valueOf()); // manoj nishad
String Primitives vs String Objects
It's generally better to use string primitives (created with literals) rather than String
objects. Here's why:
const str1 = `2 + 2`;
const str2 = new String(`2 + 2`);
console.log(eval(str1)); // 4
console.log(eval(str2)); // [String: '2 + 2']
The eval() function treats string primitives and String objects differently.
Template Literals
JavaScript Strings 2
Template literals are a modern and preferred way to create strings, especially
when you need to include variables or expressions:
const name = `Manoj Kumar`;
const mobile = 9191919191;
const about = `My name is ${name} and my mobile is ${mobile}`;
console.log(about); // My name is Manoj Kumar and my mobile is 9
String Concatenation
While you can concatenate strings using the + operator, it's generally considered
a less elegant approach:
let firstName = "Manoj";
let lastName = "Kumar";
let fullName = firstName + " " + lastName;
console.log(fullName); // Manoj Kumar
Template literals are often preferred for readability and flexibility.
String Immutability
Strings in JavaScript are immutable, meaning they cannot be changed after
creation:
let myName = new String("manoj");
console.log("Before changing ", myName[0]); // m
myName[0] = "S"; // This does not change the string
console.log("After Changing ", myName[0]); // m
String Comparisons
Strings are compared character-by-character in alphabetical order:
console.log("a" > "Z"); // true: ASCII code of 'a' is 97 and 'Z
JavaScript Strings 3
console.log("Manoj" > "Nishad"); // false: ASCII code of 'N' is
String Constructor vs String Function
There's a subtle but important difference between the String constructor and the
String function:
const a = new String("Hello world");
const b = String("Hello world");
console.log(a === "Hello world"); // false
console.log(b === "Hello world"); // true
console.log(a instanceof String); // true
console.log(b instanceof String); // false
console.log(typeof a); // object
console.log(typeof b); // string
The String constructor creates a String object, while the String function creates a
primitive string.
String Conversion
Number to String
You can convert numbers to strings in two ways:
// Way 1: Using the + operator
let counter1 = 24;
counter1 = counter1 + "";
console.log(typeof counter1); // string
// Way 2: Using the String() function
let accountId1 = 786;
accountId1 = String(accountId1);
console.log(typeof accountId1); // string
JavaScript Strings 4
String to Number
Similarly, you can convert strings to numbers:
// Way 1: Using the unary + operator
let counter2 = "34";
counter2 = +counter2;
console.log(typeof counter2); // number
// Way 2: Using the Number() function
let accountId2 = "22";
accountId2 = Number(accountId2);
console.log(typeof accountId2); // number
Conclusion
Strings are a fundamental part of JavaScript, and understanding how to create,
manipulate, and convert them is crucial for any developer.
Remember to prefer string primitives over String objects, use template literals for
complex string creation, and be aware of the immutability of strings. With these
concepts in mind, you'll be well-equipped to handle text data in your JavaScript
applications.
Happy coding!
Follow on Linkedin @manojofficialmj
Visite on Documentation Website 10xtechinfinity
Join Discord Server 10xtechinfinity
Subscribe Our YouTube Channel @bcapathshala and @10xtechinfinity
Download Source Code From GitHub @bcapathshala
JavaScript Strings 5