HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Object
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
JavaScript›Strings

Strings

String concatenation, length, uppercase/lowercase and formatting with template literals ( `${}` ).

Simple enough for a 5-year-old to understand

Concatenation and length

Example code
1
const name = "Alex";
Text.
VariableText.
2
console.log(name + " Lovelace");
+ combine with: Alex Lovelace
Output / Input+ combine with: Alex Lovelace
3
console.log(name.length);
Number of characters: 3
Output / InputNumber of characters: 3
4
console.log(name.toUpperCase());
Uppercase: ALEX
Output / InputUppercase: ALEX
VariableOutput / Input

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the word "hexlibrary" in UPPERCASE.

🎯 Expected output: HEXLIBRARY

Loading editor…

About this topic

Text is the data type most frequently dealt with in web development: user inputs, responses from the server, content written to the page. JavaScript provides a rich set of methods for texts — searching, splitting, trimming, replacing.

Texts are IMMUTABLE: when you call toUpperCase or replace, the original text does not change, a new text is returned. Forgetting to assign the result to a variable is a very common error that does not produce an error message.

Using template literals for string concatenation is now standard: you write them inside backticks and embed variables directly. Compared to concatenating with a plus sign, it is both more readable and free from type conversion surprises.

What you will learn

  • getting length with length, and characters with square brackets
  • toUpperCase, toLowerCase, trim, and replace methods
  • searching with includes, startsWith, and indexOf
  • converting between text and arrays using split and join
  • writing multi-line and variable-containing texts with template literals

When to use?

Text operations are used in form validation (trim spaces, convert to lowercase, compare), data cleaning (split comma-separated lines), and generating UI text.

When comparing text coming from a user, applying trim and toLowerCase is almost always necessary; otherwise, two values that look identical will not match.

Common mistakes

Wrong

let name = "  Ada  ";
name.trim();
console.log(name.length);

trim does not change the original text, it returns a trimmed new text. Because you didn't assign the result, name still has spaces and the length comes out greater than you expected.

Correct

let name = "  Ada  ";
name = name.trim();
console.log(name.length);

Wrong

const text = "merhaba";
text[0] = "M";
console.log(text);

Since texts are immutable, this assignment silently does nothing — in strict mode, it throws an error. To change the text, you need to generate a new text.

Correct

const text = "merhaba";
const fixed = "M" + text.slice(1);
console.log(fixed);

Wrong

const price = 10;
console.log('Fiyat: ${price}');

Template literals only work with BACKTICKS. If you use single or double quotes, variables are not embedded, and the curly braces are written out as they are. The output won't be what you expect, but you won't get an error either.

Correct

const price = 10;
console.log(`Price: ${price}`);

Frequently asked questions

What is the difference between slice and substring?

Both extract a piece from a text and in most cases give the same result. The difference appears with negative numbers: slice counts negative values from the end, while substring treats them as zero. In practice, slice is preferred because it is more predictable.

How do I check if a word exists inside a text?

The includes method returns true or false and is the most readable way. indexOf can also be used, but since it returns minus one when not found, you have to write a comparison; includes eliminates this unnecessary step.

What is the safest way to convert text to a number?

Converting with the Number function and then checking the result with Number.isNaN. This way, when the user enters letters instead of a number, you can notice it and show a meaningful message; otherwise, NaN will contaminate all calculations.

Summary

JavaScript strings are immutable: every method returns a new text, and you need to assign the result. For concatenation, template literals are standard and only work with backticks; when comparing user input, using trim with toLowerCase is almost mandatory.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code