On this page10 sections
  1. Learning Objectives
  2. 1. From ASCII to UTF-8
  3. 2. Declaring the Charset
  4. 3. What Goes Wrong Without It
  5. 4. URL Encoding
  6. 5. Encoding in JavaScript
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 028: Charsets & URL Encoding

Category5: Code, Characters & Standards
Lecture028 of 048
Estimated video length10 min
TargetHTML5 · any modern browser
LevelBeginner

Computers store text as numbers. A character set is the table that says which number means which letter. Pick the wrong one and "café" turns into "café". URLs have their own rules too: spaces and special characters must be encoded.


Learning Objectives

  • Explain what a character set is and why UTF-8 is the standard
  • Declare the charset correctly
  • Read and write URL-encoded text such as %20

1. From ASCII to UTF-8

CharsetCharactersNotes
ASCII128English letters, digits, basic punctuation. The original
ISO-8859-1 (Latin-1)256Adds Western European letters such as é and ü
UTF-8Over 1 millionEvery language, plus symbols and emojis. The web standard

UTF-8 includes all of ASCII, so plain English text is identical in both.


2. Declaring the Charset

Put this as the first thing inside <head>:

html
<meta charset="UTF-8">

Example: English, Urdu, Arabic and emojis on one page:

html
<meta charset="UTF-8">
<p>English: Hello</p>
<p lang="ur" dir="rtl">اردو: السلام علیکم</p>
<p>Français: café, crème brûlée</p>
<p>Emoji: 🎉 ✅</p>

Your editor must also save the file as UTF-8. Most editors do by default; VS Code shows it in the status bar.


3. What Goes Wrong Without It

If a UTF-8 file is read as Latin-1, each multi-byte character falls apart:

output
café   →   café
السلام   →   السلام

These garbled characters are often called "mojibake". The fix is always the same: save as UTF-8 and declare <meta charset="UTF-8">.


4. URL Encoding

A URL may only contain a limited set of ASCII characters. Anything else is written as % followed by two hexadecimal digits: the character's byte value.

CharacterEncoded
space%20 (or + in form data)
!%21
&%26
/%2F
?%3F
é%C3%A9 (two bytes in UTF-8)

Example: search forms encode for you. Type "tea & cake", submit, and look at the data sent:

html
<form action="https://www.google.com/search" target="_blank">
  <input name="q" value="tea & cake">
  <button type="submit">Search Google</button>
</form>

The URL becomes …/search?q=tea+%26+cake.


5. Encoding in JavaScript

Example

html
<p id="out"></p>
<script>
  const text = "Lahore & Karachi / 2026";
  document.getElementById("out").textContent = encodeURIComponent(text);
</script>

encodeURIComponent() encodes; decodeURIComponent() turns it back.


Common Beginner Mistakes

1. <meta charset> too late. Put it first in the <head>, before the title.

2. Spaces in file names. my photo.jpg becomes my%20photo.jpg. Use hyphens instead.

3. Building URLs by hand with special characters. Use encodeURIComponent() for values you insert.


Practice Questions

  1. What is a character set?
  2. Which character set should every web page use?
  3. Where exactly should <meta charset="UTF-8"> go?
  4. What does %20 mean in a URL?
  5. Why does "café" sometimes show up as "café"?
  6. Which JavaScript function encodes text for a URL?

Practice Answers

  1. A table that maps numbers stored by the computer to the characters they represent.
  2. UTF-8.
  3. As the first element inside <head>.
  4. A space.
  5. A UTF-8 file is being read with the wrong character set (such as Latin-1).
  6. encodeURIComponent()

Key Takeaways

  • UTF-8 covers every language and emoji; use it everywhere.
  • Declare <meta charset="UTF-8"> first in the head, and save files as UTF-8.
  • URLs encode special characters as %XX; a space is %20.
  • encodeURIComponent() does the encoding for you.