Javascript Strings 101: How to Declare, Create, and Work with Text

Table of contents
- ๐ What Exactly Is a String in Javascript?
- โ๏ธ Creating Strings: Single, Double, or Backticks?
- ๐ก Why Backticks (template literals) Are Awesome
- ๐ Common String Property: .length for Measuring text
- ๐ Accessing Individual Characters
- ๐จ Strings Are Immutable: We Can't Change Them Directly
- ๐ Checking the Type with typeof
- ๐งฑ Primitives vs. Wrapper Objects: Yep, There's a String Object Too
- ๐ง Converting Other Types to Strings
- โ Key Takeaways from Our String Journey
- ๐ Side Note: Which Primitives Have Wrapper Objects?
- ๐ฏ What's Next on Our Journey?

Strings are everywhere in Javascript. Whether we're greeting a user, labelling a button, or sending data across the internet, we're working with text values, and Javascript wraps those up in something called a string.
But a string isn't just some text inside quotes. Strings have rules, quirks, and built-in powers that let us slice, dice, and transform them in all kinds of ways.
๐ As we already introduced strings in our earlier articles, let's quickly recap what they are and then dive deeper into how we work with them in practice.
๐ What Exactly Is a String in Javascript?
A string in Javascript is a sequence of characters wrapped in quotes.
let greeting = "Hello";
let name = 'Luna';
let message = Welcome, ${name}!;
Strings can hold:
Letters:
"Hello"
Numbers (as text):
"123"
Emojis:
"๐๐"
Or... absolutely nothing:
""
(an empty string)
โ๏ธ Creating Strings: Single, Double, or Backticks?
Javascript lets us create strings using:
'single quotes'
"double quotes"
backticks (template literals) -
``
All three work, but they serve slightly different purposes:
We use single or double quotes for regular text interchangeably (just be consistent).
We use backticks when we need template literals (for multi-line text or embedding variables, also known as interpolation).
let single = 'This is single quoted';
let double = "This is double quoted";
let name = "Marco"
let template = This is a template literal embedding ${name};
You can nest quotes, but be careful with mismatches:
let quote = 'He said, "Hello!"'; // โ
valid
let wrong = 'He said, "Hello!'; // โ SyntaxError
๐ก Why Backticks (template literals) Are Awesome
Template literals are a total game-changer when building dynamic strings.
Instead of mashing variables and strings together with +
signs like it's the early 2000s, we use ${}
inside backticks (``
) to drop variables right where we want them.
let user = "Alex";
let message = Welcome back, ${user}! Ready to code?;
console.log(message); // Welcome back, Alex! Ready to code?
Try doing that with regular quotes and concatenation, and suddenly our clean code turns into a tangled mess of +
signs and quotation marks. Honestly, it's enough to make us cry. ๐ญ
โจ What's Happening Here? โ String Interpolation
The ${}
part is called string interpolation โ it's Javascript's way of saying:
"Hey, drop the value of this variable right here in the middle of the string."
Before template literals, we'd have to do this:
let message = "Welcome back, " + user + "! Ready to code?";
But with backticks, we just let Javascript handle it for us.
It's faster to write, easier to read, and we don't lose our minds juggling quotation marks.
โ Why Template Literals Rock
โ๏ธ Clean syntax โ no more messy +
signs
โ๏ธ Supports multi-line strings without weird newline characters
โ๏ธ Makes dynamic strings readable โ we see the final shape of the string clearly
๐ Common String Property: .length
for Measuring text
Every string in Javascript comes with a handy .length
property that tells us exactly how many characters the string holds โ spaces, punctuation, and all.
let word = "Javascript";
console.log(word.length); // 10
Even if the string is totally empty, .length keeps track:
console.log("".length); // 0
๐ก Where We Use This:
Password validation:
if (password.length < 8) {
console.log("Password must be at least 8 characters long.");
}
Username checks:
if (username.length < 3) {
console.log("Username must be at least 3 characters.");
}
So whether we're locking down passwords ๐ or making sure usernames aren't too short, .length
helps us enforce the rules โ no sweat, no guesswork.
๐ Accessing Individual Characters
Sometimes we need to extract a single character from a string โ perhaps to check the first letter of a username or validate a phone number prefix.
Javascript gives us two main ways to do this โ bracket notation and the .charAt()
method
let word = "Javascript";
console.log(word[0]); // "J" โ bracket notation
console.log(word.charAt(4)); // "S" โ charAt() method
๐ Quick Reminder:
Strings are zero-indexed, meaning the first character sits at position 0
.
๐ก๏ธ Real-World Example: Checking Capital Letters
Let's say we want to check if a username starts with an uppercase letter:
let input = "Sam123";
if (input[0] !== input[0].toUpperCase()) {
console.log("First letter should be uppercase.");
}
Or validate if a phone number starts with the right country code:
let phoneNumber = "+91 9876543210";
if (!phoneNumber.startsWith("+91")) {
console.log("Phone number must start with +91");
}
โ๏ธ charAt()
vs Bracket Notation โ Which One Should We Use?
โ๏ธ Bracket Notation ([]
)
Shorter and cleaner โ
Works in all modern browsers ๐
Recommended for day-to-day usage
โ๏ธ charAt()
Works in really old browsers that didn't support bracket notation (think Internet Explorer 6 ๐ฌ).
Safer if we're dealing with older environments, but honestly, we don't write for those anymore.
๐ Our Rule of Thumb:
Use bracket notation (str[0]
) unless you have a very specific reason not to.
It's clear, modern, and easier on the eyes.
๐จ Strings Are Immutable: We Can't Change Them Directly
This surprises many beginners:
Strings in Javascript are immutable, meaning we can't change a character directly.
let word = "Hello";
word[0] = "J"; // โ Doesn't work
console.log(word); // "Hello"
If we want to change a string, we must create a brand new one:
word = "Jello";
console.log(word); // "Jello"
Behind the scenes, Javascript discards the old string and builds a new one in memory.
๐ Checking the Type with typeof
As we already saw when we explored Javascript types, we can confirm whether a variable is holding a string using the typeof
operator.
let phrase = "Good morning";
console.log(typeof phrase); // "string"
This is especially useful when dealing with dynamic data, such as form inputs, API responses, or user-generated content, where we're not always sure what type of value we're working with.
When in doubt, check it. It'll save us from some weird bugs later.
๐งฑ Primitives vs. Wrapper Objects: Yep, There's a String Object Too
Most of the time, our strings are primitives โ simple, raw text values that Javascript stores efficiently.
However, Javascript also has a String object, which wraps a string in an actual object, known as a wrapper object.
let primitive = "Hello"; // string primitive
let wrapped = new String("Hello"); // String object
Check the types:
console.log(typeof primitive); // "string"
console.log(typeof wrapped); // "object"
The first one is a plain string, the second is an object.
๐ก What's a Wrapper Object?
A wrapper object is a temporary object that Javascript creates around a primitive, giving it access to methods and properties.
It's like putting a key ๐ on a keychain ๐ก๏ธ โ the key is still the same, but now it's easier to carry and use because it comes with extra features (like a flashlight or a bottle opener).
The key (primitive value) remains the same, but the keychain (wrapper object) provides us with additional tools (methods and properties).
For strings, this means that Javascript wraps the primitive in a String object behind the scenes, allowing us to call methods like .toUpperCase()
or access the .length
property.
๐ค Should We Ever Use new String()?
Almost never.
Javascript automatically wraps string primitives behind the scenes whenever we use string methods or properties.
For example, when we do this:
let name = "Luna";
console.log(name.toUpperCase()); // "LUNA"
Or this:
console.log(name.length); // 4
...Javascript temporarily wraps the primitive string in a String object, lets us use its methods like .toUpperCase()
, .toLowerCase()
, .trim()
(we'll explore these soon), or properties like .length
, and then quietly discards the wrapper.
The takeaway?
Even though we're working with primitive values, we still get to use powerful object-like behaviour, and Javascript handles the messy details for us.
โ๏ธ Our Rule of Thumb:
Stick to simple strings ("Hello"
). They're faster, simpler, and what most real-world code uses.
Avoid new String()
unless we're working with some niche edge case.
๐ง Converting Other Types to Strings
If we need to turn numbers, booleans, or other types into strings, we use:
String(42); // "42"
(123).toString(); // "123"
true.toString(); // "true"
โ ๏ธ Be careful:
null.toString()
orundefined.toString()
will throw errors.We use
String()
for safer conversions:
console.log(String(null)); // "null"
console.log(String(undefined)); // "undefined"
โ Key Takeaways from Our String Journey
โ๏ธ We use 'single'
, "double"
, or backticks to declare strings.
โ๏ธ Strings are immutable โ modifying them creates a new one.
โ๏ธ We use .length
, bracket notation, and .charAt()
to work with characters.
โ๏ธ We confirm types with typeof
.
โ๏ธ We let Javascript handle the String object behind the scenes, and stick with plain strings.
โ๏ธ We convert other types to strings using String()
or .toString()
when needed.
๐ Side Note: Which Primitives Have Wrapper Objects?
Javascript has wrapper objects for these types:
Primitive Type | Wrapper Object |
number | Number |
string | String |
boolean | Boolean |
symbol | Symbol |
bigint | BigInt |
โ ๏ธ Note: null
and undefined
do not have wrapper objects โ they're considered primitive-only, with no built-in methods.
So next time we call a string method, Javascript quietly wraps our primitive in an object, lets us use the method, and then throws the wrapper away when it's done. Neat, right?
๐ฏ What's Next on Our Journey?
We've only scratched the surface of what strings can do.
Next, let's dive into the string methods we use every day โ trimming the junk, searching for what matters, and combining text like professionals.
๐ Stay tuned for "Essential String Methods in Javascript: Trimming, Searching, and Combining Text and many more."
Subscribe to my newsletter
Read articles from Sangy K directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sangy K
Sangy K
An Introvert Coder | Trying to be a Full-stack Developer | A Wannabe Content Creator