How to Display JavaScript Variable Value in Alert Box ? Last Updated : 04 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report An alert box is a dialog box that pops up on the screen to provide information to the user. It contains a message and an OK button, which allow users to acknowledge the message and close the box. Alert boxes are commonly used in web development to display important messages, warnings, or notifications to users. ApproachFirst, create your HTML and CSS structure along with a button.In the javascript code create some variables and handle the "click" event of the button.In the function provide the alert method and pass the variable.You can use the backtick for concat multiple variables.Syntax:let var1 = something1; // Write your first variablelet var2 = somethimg2; // Write your second variablealert(var1+var2);Example: The below example shows how to display JavaScript variable value in an alert box. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Display Variable Value</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f4f4f4; } .container { text-align: center; background-color: #fff; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); position: relative; } .container::before, .container::after { content: ''; position: absolute; top: -5px; left: -5px; right: -5px; bottom: -5px; border-radius: 15px; z-index: -1; } .container::before { border: 2px solid #007bff; } .container::after { border: 2px solid #45d719; } #main-heading { margin-bottom: 20px; } #alert_btn { padding: 10px 20px; font-size: 16px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } #alert_btn:hover { background-color: #0056b3; } </style> </head> <body> <div class="container"> <h1 id="main-heading"> How to display JavaScript variable value in alert box </h1> <button id="alert_btn">Show Variable</button> </div> <script> let alert_btn = document.getElementById("alert_btn"); let a = "Geeks"; let b = "for"; let c = "geeks"; alert_btn.addEventListener("click", function () { alert(`Your variable value is ${a + b + c}`); }); </script> </body> </html> Output: Output Comment More infoAdvertise with us Next Article How to Use Dynamic Variable Names in JavaScript? S skaftafh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to display error without alert box using JavaScript ? JavaScript errors are displayed using alert boxes, which can interrupt user experience and feel outdated. But Now, there are many alternatives to alert boxes for displaying error messages in a more user-friendly way. This article covers different methods to display errors dynamically without using a 3 min read How to Display Image in Alert/Confirm Box in JavaScript ? This article explores how to display an image in an alert box to enhance the user experience with a visually appealing interface. The standard JavaScript alert() method doesn't support images. Table of Content By creating a custom alert box in JavaScriptUsing SweetAlert LibraryBy creating a custom a 3 min read How to Display Image in Alert/Confirm Box in JavaScript ? Using images in alert or confirm boxes in JavaScript adds visual effect and shows the clarity of alert type. With the help of images in alert, messages become more memorable, and accessible, and can attract the user's attention, and the overall user experience is good for use. ApproachAt first, crea 3 min read How to Use Dynamic Variable Names in JavaScript? Dynamic variable names are variable names that are not predefined but are generated dynamically during the execution of a program. This means the name of a variable can be determined at runtime, rather than being explicitly written in the code.Here are different ways to use dynamic variables in Java 2 min read How to Edit a JavaScript Alert Box Title ? We can't directly modify the title of the alert box because the title is controlled by the browser and cannot be changed by JavaScript. However, we can create a custom alert box. Table of Content Using a Custom Alert Box FunctionUsing SweetAlert LibraryUsing a Custom Alert Box FunctionIn this approa 2 min read How to Pass variables to JavaScript in Express JS ? Express is a lightweight framework that sits on top of Node.jsâs web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your applicationâs functionality with middleware and routing. When working with Express.js you may encounter scenarios where you 2 min read Like