← Back to JavaScript Course | Chapter 3: Strings, Arrays & Data | Lesson 1 of 6

JS Strings

A string is simply text, a name, a sentence, a URL, wrapped so JavaScript knows to treat it as words rather than a calculation or a command. You can build strings with single quotes, double quotes, or the more flexible backtick template literals, which let you embed variables directly inside the text without messy joining. Strings show up constantly in real code, displaying a tutorial title, building a message, or reading a URL, all things happening behind the scenes across cookiescursor.com every time a page loads.
Syntax
javascript
let text = "value";
let text = 'value';
let text = `template ${expression}`;
text.length;

Creating Strings

Strings can be created with single quotes, double quotes, or backticks, and all three behave the same for basic text, the choice is largely a matter of consistency and, for backticks, added features.

Note: Pick one quote style, single or double, for regular strings and stick with it consistently across a project for readability.
Warning: A string must start and end with the same type of quote, mismatching them causes an immediate syntax error.

उदाहरण: Creating Strings

javascript
// Declare the variable `a`, set to 'Single quotes'
// Declare the variable `a`, set to 'Single quotes'
let a = 'Single quotes';
// Declare the variable `b`, set to "Double quotes"
// Declare the variable `b`, set to "Double quotes"
let b = "Double quotes";
// Declare the variable `c`, set to `Backticks`
// Declare the variable `c`, set to `Backticks`
let c = `Backticks`;
// Print `a, b, c` to the console
// Print `a, b, c` to the console
console.log(a, b, c);

Template Literals

Template literals, written with backticks, let you embed variables and expressions directly inside a string using ${expression}, avoiding the need to join text together with the + operator.

Note: Template literals also support multi-line strings naturally, just press enter inside the backticks without needing a special escape character.
Warning: The ${} syntax only works inside backtick strings, using it inside single or double quotes just produces the literal characters, not an embedded value.

उदाहरण: Template Literals

javascript
// Declare the constant `name`, set to "Maya"
// Declare the constant `name`, set to "Maya"
const name = "Maya";
// Declare the constant `age`, set to `25`
// Declare the constant `age`, set to `25`
const age = 25;
// Print `${name} is ${age} years old.` to the console
// Print `${name} is ${age} years old.` to the console
console.log(`${name} is ${age} years old.`);

String Length

Every string has a length property that returns the number of characters it contains, counting letters, numbers, spaces, and punctuation all equally.

Note: length is a property, not a method, so it's accessed without parentheses, myString.length, not myString.length().
Warning: An empty string still has a length of 0, not undefined, which is worth remembering when checking whether a string has content.

उदाहरण: String Length

javascript
const message = "Hello!";
console.log(message.length);

Escape Characters

Escape characters, starting with a backslash, let you include special characters inside a string, like \n for a new line, \t for a tab, or \' to include a quote that would otherwise end the string early.

Note: Template literals with backticks avoid many escape character needs, since you can write real line breaks directly inside them.
Warning: Forgetting to escape a quote that matches the string's delimiter, like an apostrophe in dont', ends the string prematurely and causes a syntax error.

उदाहरण: Escape Characters

javascript
console.log("Line one\nLine two");
console.log("She said \"hello\"");

Comparing Strings

Strings can be compared with equality and relational operators, comparing them character by character based on their character codes, with strict equality === checking both value and type together.

Note: String comparisons are case sensitive by default, Apple and apple are not considered equal.
Warning: Comparing strings with < and > compares them alphabetically by character code, which can produce surprising results with mixed uppercase and lowercase letters.

उदाहरण: Comparing Strings

javascript
// Print "Apple" === "apple" to the console
// Print "Apple" === "apple" to the console
console.log("Apple" === "apple");
// Print "apple" === "apple" to the console
// Print "apple" === "apple" to the console
console.log("apple" === "apple");
// Print "apple" < "banana" to the console
// Print "apple" < "banana" to the console
console.log("apple" < "banana");
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.