Introduction
A String in JavaScript is a sequence of characters used to represent textual data. Strings are one of the most commonly used data types and are essential for storing and manipulating text such as names, addresses, emails, URLs, passwords, messages, and API responses.
Almost every JavaScript application works with strings. Whether you’re building a website, creating a Node.js application, or developing automation scripts using Selenium, Playwright, or Cypress, you’ll frequently work with string values.
In JavaScript, strings are immutable, which means that once a string is created, its characters cannot be changed. However, you can create a new string based on an existing one.
What is a String?
A string is a collection of zero or more characters enclosed within:
-
Single quotes (
' ') -
Double quotes (
" ") -
Backticks (
` `)
A string can contain:
-
Letters
-
Numbers
-
Symbols
-
Spaces
-
Unicode characters
-
Emojis
Why Do We Use Strings?
Strings are used to:
-
Store user names
-
Store passwords
-
Store URLs
-
Display messages
-
Process API responses
-
Handle JSON data
-
Read file contents
-
Validate web page text
-
Perform text manipulation
Creating Strings Using Single Quotes
A string can be created using single quotes.
let language = 'JavaScript';
console.log(language);
Output
JavaScript
Creating Strings Using Double Quotes
Double quotes work exactly the same as single quotes.
let framework = "Playwright";
console.log(framework);
Output
Playwright
Creating Strings Using Backticks
Backticks create template literals, which allow multi-line strings and string interpolation.
let course = `JavaScript Tutorial`;
console.log(course);
Output
JavaScript Tutorial
Empty String
An empty string contains no characters.
let message = "";
console.log(message);
Output
String Containing Numbers
Numbers enclosed in quotes become strings.
let mobile = "9876543210";
console.log(mobile);
Output
9876543210
String Containing Special Characters
Strings can include special characters.
let password = "@dm!n123";
console.log(password);
Output
@dm!n123
Multi-Line Strings Using Backticks
Backticks allow strings to span multiple lines.
let address = `Bengaluru
Karnataka
India`;
console.log(address);
Output
Bengaluru
Karnataka
India
String Object
Although strings are usually created as primitive values, they can also be created using the String constructor.
let city = new String("Mumbai");
console.log(city);
Output
[String: 'Mumbai']
Note: Using the
Stringconstructor is generally not recommended. Prefer string literals because they are simpler and more efficient.
Checking the Data Type
Use the typeof operator to verify that a value is a string.
let company = "OpenAI";
console.log(typeof company);
Output
string
Real-World Example
Suppose an e-commerce application stores product details.
let productName = "Wireless Mouse";
console.log(productName);
Output
Wireless Mouse
Another example:
let website = "https://example.com";
console.log(website);
Output
https://example.com
Automation Testing Example
Strings are extensively used in automation testing for handling test data, locators, URLs, assertions, and API responses.
Playwright Example
Opening a website.
const url = "https://opensource-demo.orangehrmlive.com/";
await page.goto(url);
Selenium Example
Validating the page title.
const expectedTitle = "OrangeHRM";
const actualTitle = await driver.getTitle();
console.log(expectedTitle === actualTitle);
Output
true
Cypress Example
Visiting an application.
const baseUrl = "https://example.com";
cy.visit(baseUrl);
API Testing Example
Validating a JSON response.
const expectedMessage = "Success";
const response = {
message: "Success"
};
console.log(response.message === expectedMessage);
Output
true
Data-Driven Testing Example
Using string test data.
const usernames = [
"admin",
"manager",
"tester"
];
console.log(usernames);
Output
[ 'admin', 'manager', 'tester' ]
Common Mistakes
Forgetting Quotes
Incorrect:
let name = JavaScript;
This causes a ReferenceError because JavaScript treats JavaScript as a variable instead of a string.
Correct:
let name = "JavaScript";
Mixing Quotes Incorrectly
Incorrect:
let message = "It's JavaScript";
This works correctly because the apostrophe is inside double quotes.
However,
let message = 'It's JavaScript';
produces a syntax error because the apostrophe ends the string.
Correct:
let message = "It's JavaScript";
or
let message = 'It\'s JavaScript';
Using new String()
Avoid:
let language = new String("JavaScript");
Prefer:
let language = "JavaScript";
String literals are simpler and more efficient.
Best Practices
Prefer String Literals
Create strings using quotes instead of the String constructor.
const language = "JavaScript";
Use const for Fixed Strings
If the value never changes, declare it using const.
const browser = "Chrome";
Use Meaningful Variable Names
Instead of:
let a = "Chrome";
Use:
let browserName = "Chrome";
This improves readability.
Use Backticks for Dynamic Text
When constructing strings with variables or multiple lines, use template literals.
const user = "John";
console.log(`Welcome ${user}`);
Conclusion
Strings are one of the most fundamental data types in JavaScript and are used to represent textual information. JavaScript provides multiple ways to create strings using single quotes, double quotes, and backticks, with template literals offering additional flexibility for dynamic and multi-line text.
For automation engineers, strings are essential for working with URLs, locators, test data, assertions, API responses, JSON data, and browser interactions. A solid understanding of string creation forms the foundation for learning more advanced string operations such as indexing, slicing, searching, replacing, and formatting.
Frequently Asked Questions (FAQs)
What is a string in JavaScript?
A string is a sequence of characters used to represent text.
How can you create a string in JavaScript?
You can create a string using:
-
Single quotes (
' ') -
Double quotes (
" ") -
Backticks (
` `)
Which is the recommended way to create strings?
Using string literals with single quotes, double quotes, or backticks is recommended instead of the String constructor.
What is the difference between string literals and new String()?
-
String literals create primitive string values.
-
new String()creates a String object, which is rarely needed.
Why are strings important in automation testing?
Automation engineers use strings for:
-
URLs
-
Locators
-
Test data
-
Assertions
-
API responses
-
JSON parsing
-
File handling
-
Browser interactions
Key Takeaways
-
A string is a sequence of characters used to represent text.
-
Strings can be created using single quotes, double quotes, or backticks.
-
Backticks support template literals and multi-line strings.
-
Strings in JavaScript are immutable.
-
Use
typeofto check whether a value is a string. -
Prefer string literals over the
Stringconstructor. -
Use meaningful variable names for better readability.
-
Use
constfor strings that do not change. -
Strings are heavily used in JavaScript applications and automation testing.
-
Understanding string creation is the first step toward mastering string manipulation in JavaScript.
