Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Difference between decodeURIComponent() and decodeURI() functions in JavaScript
Next article icon

JavaScript encodeURI(), decodeURI() and its components Functions

Last Updated : 17 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The encodeURI() and decodeURI() functions in JavaScript are used to handle URI (Uniform Resource Identifier) encoding and decoding. They ensure that URIs are properly formatted for web usage, converting characters that may cause issues into a valid, encoded format.

1. encodeURI() Function

The encodeURI() function encodes a URI by replacing certain characters with their UTF-8 escape sequences. It preserves characters that are valid in a URI, like :, /, ?, and #.

JavaScript
let uri = "https://example.com/query?name=Amit Kumar&age=25"; let encoded = encodeURI(uri); console.log(encoded); 

Output
https://example.com/query?name=Amit%20Kumar&age=25 

encodeURI() converts the space in "Amit Kumar" into %20 but leaves the ?, =, and & characters intact, as they are part of the URI syntax.

2. decodeURI() Function

The decodeURI() function decodes an encoded URI by replacing escape sequences with their original characters.

JavaScript
let uri = "https://example.com/query?name=Amit%20Kumar&age=25"; let decoded = decodeURI(uri); console.log(decoded); 

Output
https://example.com/query?name=Amit Kumar&age=25 

decodeURI() converts the %20 back into a space, restoring the original URI.

encodeURIComponent() and decodeURIComponent()

In addition to encodeURI() and decodeURI(), JavaScript provides the encodeURIComponent() and decodeURIComponent() functions, which operate on individual components of a URI.

1. encodeURIComponent()

Encodes a URI component (such as query string parameters or path segments) and encodes characters like &, =, ?, and others.

JavaScript
let name = "Amit Kumar"; let encoded = encodeURIComponent(name); console.log(encoded); 

Output
Amit%20Kumar 

encodeURIComponent() encodes the space in "Amit Kumar" into %20, as it treats each component as part of a larger URI.

2. decodeURIComponent()

Decodes a URI component back into its original format.

JavaScript
let encoded = encodeURIComponent("Amit Kumar"); let decoded = decodeURIComponent(encoded); console.log(decoded); 

Output
Amit Kumar 

decodeURIComponent() decodes %20 back into a space, restoring the original string.

Advantages

  • Ensures that special characters in a URI (e.g., spaces, @, &) are encoded into a format suitable for transmission over the web.
  • Prevents unintended behavior caused by reserved characters.
  • Simplifies the process of handling URLs in web applications, making it easier to pass data between the client and server.
  • encodeURI() preserves query-related characters (?, #, &, /) while encoding others, making it suitable for encoding entire URLs.

Key Differences:

FunctionPurposeEncodes/Decodes
encodeURI()

Encodes a full URI, leaving URI delimiters (:, /, ?, &) intact

Encodes non-URI characters
decodeURI()Decodes a full URI, reversing percent-encoding for non-URI charactersDecodes percent-encoded URI
encodeURIComponent()Encodes individual URI components (query parameters, path segments)Encodes all characters
decodeURIComponent()Decodes individual URI components back into their original formDecodes percent-encoded component
  • Use encodeURI() and decodeURI() when dealing with full URIs, ensuring that the structure and syntax are preserved while encoding/decoding.
  • Use encodeURIComponent() and decodeURIComponent() when working with individual URI components, such as query string parameters, to ensure special characters are properly encoded or decoded.

Next Article
Difference between decodeURIComponent() and decodeURI() functions in JavaScript

N

neerajnegi174
Improve
Article Tags :
  • Misc
  • Technical Scripter
  • JavaScript
  • Web Technologies
  • javascript-functions
Practice Tags :
  • Misc

Similar Reads

  • What are the encodeURI() and decodeURI() in JavaScript ?
    URLs and URIs are designed to locate/identify resources available over the internet, anything that uniquely identifies a resource is its URI, such as id, name. A URL specifies a resource and its access protocol. All URLs are URIs, but not all URIs are URLs. URI can only have certain characters from
    3 min read
  • Difference between decodeURIComponent() and decodeURI() functions in JavaScript
    Both decodeURI() and decodeURIComponent() are Javascript global functions that are used for decoding encoded URI (Uniform Resource Identifier). JavaScript decodeURI() function: It decodes a string previously encoded by the encodeURI() function. It returns a decoded URI by replacing each UTF-8 escape
    2 min read
  • Difference Between encodeURI and encodeURIComponent in JavaScript
    The encodeURI and encodeURIComponent functions are used to encode a URI by transforming characters that could otherwise cause issues when included in a URI. While both functions are used for encoding, they serve different purposes and encode different sets of characters. Understanding the difference
    2 min read
  • Difference between unescape() and escape() functions in JavaScript
    In this article, we will learn about the escape() & unescape() functions in JavaScript. We will understand the purpose of using both these functions through the example. Later in this article, we will discuss the difference between escape() & unescape() functions. Let's discuss the escape()
    4 min read
  • How to Encode and Decode a URL in JavaScript?
    Encoding and decoding URLs in JavaScript is essential for web development, especially when making GET requests with query parameters. This process ensures special characters in URLs are correctly interpreted by the server. For instance, spaces are converted to %20 or + in URLs. This guide covers how
    5 min read
  • Encode and Decode Strings in Java with JUnit Tests
    Strings are very useful and they can contain sequences of characters and there are a lot of methods associated with Strings. Moreover, encoding and decoding are possible for a given use case and it can be validated whether are they equal or not for a given requirement. They can be tested for validit
    4 min read
  • When we use Escape Instead of encodeURI / encodeURIComponent in JavaScript ?
    A URL consists of many characters, both special and unique. Unique characters include those that are not plain standard such as spaces etc. So it means that we need to encode characters in UTF-8. So if we have a string such as: "https://www.gfg.com/what is html?" then UTF-8 will encode it as "https:
    3 min read
  • JavaScript | Encode/Decode a string to Base64
    To encode or decode strings in JavaScript, we can use the built-in functions provided by the language. These functions help in encoding special characters in a URL or decoding encoded strings back to their original form. 1. btoa() MethodThis method encodes a string in base-64 and uses the "A-Z", "a-
    6 min read
  • PHP mb_convert_encoding() Function
    The mb_convert_encoding() function is an inbuilt function in PHP that transforms the string into another character encoding. Syntax: mb_convert_encoding( array|string $string, string $to_encoding, array|string|null $from_encoding = null ): array|string|falseParameters: This function has 3 parameters
    1 min read
  • How to Encode and Decode a URL in PHP?
    Encoding and decoding URLs in PHP are essential tasks when working with web applications, especially for handling user input and constructing query strings. URL encoding ensures that special characters are properly represented in URLs, preventing errors or unexpected behavior during data transmissio
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences