Home » JavaScript String

JavaScript String

JavaScript String

Need of Strings

  • Lots of information we store or Show on website, is actually stored as a string in JavaScript
  • For Example, when you type anything in the Google search box, that is a string, every product name you see on Amazon is also a string, and every restaurant name you see on Zomato is also a string.
    javascript-string-example.png
  • For Example :- You have received notifications from Any Social Network App.

    Here, we dynamically change the name for the user. We use strings to achieve this. We will learn how to do this in these class.

    synamically-change-string-javascript.png

Output: –

let recipient_name = "Matt"
// Dynamic notification content
let notification_content = `${recipient_name}, sent you a friend request.`
// Send the notification content
Matt, sent you a friend request.
JavaScript
Matt, sent you a friend request.
JavaScript

What is Strings?

  • The string is a group of characters.
  • It can include a-z, A-z, 0-9, and also all special characters like #,@,$, etc. In total, we have 256 characters.
  • Each character in a string has an index, Starting from 0 to the length of the string.
  • Strings are texts, which are under single, double, and back-tick quotes. To declare a string, we need a variable name, an assignment operator, a value under a single quote, double quote, or backtick quote How to declare a String?
let s1 = "Geekster";
let s2 = 'Geekster';
let s3 = `Geekster`;
JavaScript

They create practically the same string.

console.log(s1===s2) // true
console.log(s2===s3) // true
console.log(s1===s3) // true
JavaScript

String Properties in JavaScript

Long Literal String

A string could be a single character or paragraph or a page. If the string length is too big it does not fit in one line. We can use the backslash character () at the end of each line to indicate that the string will continue on the next line. Example:

let paragraph = "My name is Anuj. I live in India.
I am a teacher and I love teaching. I teach HTML, CSS, JavaScript, React, Redux, 
Node.js, Python, Data Analysis and D3.js for anyone who is interested to learn. 
In the end of 2019, I was thinking to expand my teaching and to reach 
to global audience and I started a Python challenge from November 20 - December 19.
It was one of the most rewarding and inspiring experience.
Now, we are in 2020. I am enjoying preparing the 30DaysOfJavaScript challenge and 
I hope you are enjoying too."

console.log(paragraph)
JavaScript

String Concatenation

When you log in to your Amazon account, it displays a greeting like ‘Hello, [Your Name].

string-concatination.png

You can do this by using String Concatenation, let’s see:

String Concatenation can be done by using :- → ‘+’ operation → using Template Strings

Using ‘+’ Operator

Concatenating using the addition operator is an old way. This way of concatenating is tedious and error-prone. It is good to know how to concatenate this way, but I strongly suggest to use the ES6 template strings

let greeting = "Hello,"
let username = "anuj";

let message = greeting +" "+username;
console.log(message); // Hello, anuj
JavaScript

Using Template String

To create a template string, we use two back-ticks. We can inject data as expressions inside a template string. To inject data, we enclose the expression with a curly bracket({}) preceded by a $ sign. See the syntax below.

let greeting = "Hello,"
let username = "anuj";

let message = `${greeting} ${username}`;
console.log(message); // Hello, anuj
JavaScript

Example:2

let firstName = 'Anuj'
let lastName = 'Negi'
let country = 'India'
let city = 'Delhi'
let language = 'JavaScript'
let job = 'TPM'
let age = 21
let fullName = firstName + ' ' + lastName

let personInfo1 = `I am ${fullName}. I am ${age}. I live in ${country}.` //ES6 - String interpolation method
let personInfo2 = `I am ${fullName}. I live in ${city}, ${country}. I am a ${job}. I teach ${language}.`
console.log(personInfo1)
console.log(personInfo2)
JavaScript

Output:-

I am Anuj Negi. I am 21. I live in India.
I am Anuj Negi. I live in Delhi, India. I am a TPM. I teach JavaScript.
JavaScript

Use of length Property

You have used a basic website that provides a character count for the text you input. The question is, how does it determine the character count as you type? Let’s take a look.

length-property.png

you can use here length property of String.

let s = "I love Geekster";
console.log(s.length); // 15
JavaScript

Conclusion

JavaScript strings are an important part of handling text data in web development. They enable developers to manipulate, integrate, search, and manipulate content in applications. It’s important to understand string methods and functionality in order to have a robust and interactive web experience. As JavaScript continues to evolve, thread optimization skills empower developers to create more efficient and user-friendly applications.

Frequently Asked Questions

1. How do you construct a string in JavaScript?

Create a string by providing single (‘) or double (“) quotes.

2. How do you find the length of a string?

Use the .length property to get the number of characters in the string.

3. How do you connect the wires?

Strings can be concatenated using the + operator or the concat() method.

4. Are JavaScript strings reversible?

No, JavaScript strings are immutable. Methods that modify a string return a new string without modifying the original.