Last modified: May 13, 2026 By Alexander Williams
JavaScript Variable as String Guide
In JavaScript, a string is a sequence of characters. It is one of the most common data types. You can store text in a variable as a string. This guide will show you how to work with strings in JavaScript.
Strings are everywhere. You use them for user input, messages, and data. Understanding how to use a JavaScript variable as a string is key for any developer.
How to Create a String Variable
You can create a string variable using three ways. Use single quotes, double quotes, or backticks. All three work, but backticks have extra power.
// Using single quotes
let name = 'John';
// Using double quotes
let greeting = "Hello World";
// Using backticks (template literals)
let message = `Welcome to JavaScript`;
The output for each is the same. They all store text. But backticks let you embed expressions.
John
Hello World
Welcome to JavaScript
String Concatenation
You can join strings together. This is called concatenation. Use the + operator. It is simple and works everywhere.
let firstName = 'Jane';
let lastName = 'Doe';
let fullName = firstName + ' ' + lastName;
console.log(fullName);
Jane Doe
Concatenation is useful. But it can get messy with many variables. Use template literals for cleaner code.
Template Literals for Strings
Template literals use backticks. They allow embedding variables directly. Use ${} to insert a variable. This makes your code more readable.
let age = 25;
let sentence = `I am ${age} years old.`;
console.log(sentence);
I am 25 years old.
Template literals also support multi-line strings. No need for \n or line breaks.
let poem = `Roses are red,
Violets are blue,
JavaScript is fun,
And so are you.`;
console.log(poem);
Roses are red,
Violets are blue,
JavaScript is fun,
And so are you.
Converting Variables to Strings
Sometimes you have a number or boolean. You need to convert it to a string. Use the String() function or the toString() method.
let num = 100;
let bool = true;
let strNum = String(num);
let strBool = bool.toString();
console.log(strNum);
console.log(strBool);
console.log(typeof strNum);
console.log(typeof strBool);
100
true
string
string
Both methods work. String() is safer for null or undefined. toString() throws an error on those.
Useful String Methods
JavaScript has many built-in string methods. They help you manipulate text. Here are a few important ones.
length
The length property gives the number of characters. It is not a method, but very useful.
let text = 'JavaScript';
console.log(text.length);
10
toUpperCase and toLowerCase
Use toUpperCase() to make all letters uppercase. Use toLowerCase() for lowercase.
let word = 'Hello';
console.log(word.toUpperCase());
console.log(word.toLowerCase());
HELLO
hello
includes
The includes() method checks if a string contains a substring. It returns true or false.
let sentence = 'The quick brown fox';
console.log(sentence.includes('fox'));
console.log(sentence.includes('cat'));
true
false
slice
Use slice() to extract a part of a string. It takes start and end indexes.
let str = 'Hello World';
let part = str.slice(0, 5);
console.log(part);
Hello
Escape Characters in Strings
Sometimes you need special characters. Use backslash \ to escape. For example, to add a quote inside a string.
let quote = "She said, \"JavaScript is great!\"";
console.log(quote);
She said, "JavaScript is great!"
Common escape sequences include \n for new line and \t for tab.
String Interpolation with Variables
String interpolation is powerful. It lets you build strings dynamically. Combine it with expressions for complex strings.
let x = 10;
let y = 20;
let result = `The sum of ${x} and ${y} is ${x + y}.`;
console.log(result);
The sum of 10 and 20 is 30.
This is cleaner than concatenation. It reduces errors and improves readability.
Common Mistakes with String Variables
Beginners often mix types. For example, adding a number to a string. JavaScript converts the number to a string. This is called type coercion.
let age = 25;
let message = 'Age: ' + age;
console.log(message);
console.log(typeof message);
Age: 25
string
This can cause bugs. Always be aware of your variable types. Use typeof to check.
Strings and Variable Scope
Strings behave like other variables in scope. A string declared inside a function is local. Outside, it is global. For more details, read our JavaScript Variable Scope Explained guide.
Remember that strings are immutable. You cannot change a string directly. You must create a new one.
Comparing Strings
Use === to compare strings. It checks both value and type. Case matters.
let a = 'Hello';
let b = 'hello';
console.log(a === b);
console.log(a.toLowerCase() === b.toLowerCase());
false
true
For case-insensitive comparison, convert both to same case first.
Best Practices for String Variables
Use const for strings that do not change. Use let for strings that will change. Avoid var in modern code. For a complete overview, see our JavaScript Variable Declaration Guide.
Always use template literals for complex strings. They are easier to read and maintain.
Conclusion
Strings are fundamental in JavaScript. You now know how to create, manipulate, and convert them. Use template literals for cleaner code. Use methods like includes() and slice() to work with text. Remember to check variable types to avoid bugs. Practice with examples to master strings. For more on variable types, check our JavaScript Variable Types Guide. Happy coding!