Decode Base64 Encoded String

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to decode a string of data which has been encoded using base-64 encoding in JavaScript. You will learn how to create a Buffer for the given string with base-64 encoding and how to use Buffer.prototype.toString() to return the decoded string. This lab will provide hands-on experience in decoding base64 encoded strings in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") subgraph Lab Skills javascript/variables -.-> lab-28259{{"`Decode Base64 Encoded String`"}} javascript/data_types -.-> lab-28259{{"`Decode Base64 Encoded String`"}} javascript/comp_ops -.-> lab-28259{{"`Decode Base64 Encoded String`"}} end

Decoding Base64 Encoded String

To decode a string of data that has been encoded using base-64 encoding, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Create a Buffer for the given string with base-64 encoding.
  3. Use Buffer.prototype.toString() to return the decoded string.

Here's an example code snippet:

const atob = (str) => Buffer.from(str, "base64").toString("binary");

You can test this function by running atob('Zm9vYmFy') which should return 'foobar'.

Summary

Congratulations! You have completed the Decode Base64 Encoded String lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like