DEV Community

Tooleroid
Tooleroid

Posted on

Generate QR Codes in JavaScript: Step-by-Step Guide

QR codes have become a vital tool in modern web development, enabling seamless sharing of information such as URLs, contact details, and more. In JavaScript, creating QR codes is both easy and versatile, thanks to libraries and APIs.

In this guide, you’ll learn:

  • What QR codes are and their common use cases.
  • How to generate QR codes in JavaScript using libraries.
  • How to customize QR codes for your projects.

What Are QR Codes?

QR (Quick Response) codes are two-dimensional barcodes that can store information such as text, URLs, or contact data. They're widely used in:

  • Mobile payments (e.g., Venmo, PayPal).
  • Marketing campaigns (e.g., flyers, posters).
  • Secure login and data sharing.

Generating QR Codes in JavaScript

JavaScript offers various ways to generate QR codes, including libraries like qrcode.js and qr-code-styling. Let’s dive into how to use these tools.

Option 1: Using qrcode.js

qrcode.js is a lightweight library for generating QR codes. Here's how you can use it:

  1. Install the Library

Include the library in your project via a CDN:

<script src="https://cdn.jsdelivr.net/npm/qrcode/build/qrcode.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

Or install via npm:

npm install qrcode
Enter fullscreen mode Exit fullscreen mode
  1. Generate a Simple QR Code
<!DOCTYPE html>
<html lang="en">
<head>
  <title>QR Code Generator</title>
</head>
<body>
  <div id="qrcode"></div>
  <script>
    const QRCode = require('qrcode');
    QRCode.toCanvas(document.getElementById('qrcode'), 'https://example.com', function (error) {
      if (error) console.error(error);
      console.log('Success!');
    });
  </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Option 2: Using qr-code-styling

qr-code-styling allows for more customization, such as changing colors, shapes, and sizes.

  1. Install the Library
npm install qr-code-styling
Enter fullscreen mode Exit fullscreen mode
  1. Create a QR Code
import QRCodeStyling from "qr-code-styling";

const qrCode = new QRCodeStyling({
  width: 300,
  height: 300,
  data: "https://example.com",
  dotsOptions: {
    color: "#4267b2",
    type: "rounded"
  },
  backgroundOptions: {
    color: "#ffffff"
  }
});

qrCode.append(document.getElementById("canvas"));
Enter fullscreen mode Exit fullscreen mode
  1. Add Customization
qrCode.update({
  data: "https://newexample.com",
  dotsOptions: {
    color: "#ff5733"
  }
});
Enter fullscreen mode Exit fullscreen mode

Real-World Applications

  1. Dynamic QR Codes:

    Generate QR codes for user-specific data, such as tickets or authentication tokens.

  2. Custom Designs for Branding:

    Use libraries like qr-code-styling to create QR codes that match your brand's aesthetic.

  3. Web App Integration:

    Add QR codes to your apps for features like sharing links, payments, or Wi-Fi credentials.

Conclusion

Generating QR codes in JavaScript is straightforward with libraries like qrcode.js and qr-code-styling. Whether you need simple QR codes or highly customized designs, JavaScript provides the tools to implement them efficiently.

Start integrating QR codes into your projects today, and make sharing information more interactive and accessible!

You can also check out our QR Code Generator tool to generate QR codes in JavaScript.

Top comments (0)