← Guides

Developer Guide

URL Encoding in JavaScript

Learn URL encoding in JavaScript. Understand when to use encodeURI(), encodeURIComponent(), and handle special characters.

javascript url encodeencodeuriencodeuricomponentjs url encodingjavascript escape url

JavaScript provides two built-in functions for URL encoding with different use cases.

encodeURIComponent()

Encodes everything except: A-Za-z0-9 - _ . ! ~ * ' ( ). Use this for query parameters and path segments.

encodeURI()

Preserves URL structure characters like : / ? # @ etc. Use this only for full URLs that already have valid structure.

Decoding

Use decodeURIComponent() and decodeURI() to reverse the encoding. Always decode before re-encoding to avoid double encoding.

Summary

Test your URL encoding with our free online URL Encoder/Decoder.

Try it yourself

Put what you've learned into practice with our free browser-native tool.

Open Tool →