How to Code an Alert with a Variable Using Javascript

Part 1 of 2:

Setting Up the Variable

  1. Write the first line of code. Type "var username = prompt ('What is your name?');". Replace "username" with whatever you want to call your variable. Replace "What is your name?" with whatever you want to ask for.
Part 2 of 2:

Making the Alert

  1. How to Code an Alert with a Variable Using Javascript Picture 1
    Write the next line of code. Type "alert ("Hey, " + name + "!");". This line of code will add the variable "name" to the word "Hey, "(with the space at the end), and then add "!" to end the sentence (not required). For example, if the user inputs "Trevor" as the value of the variable "name", the alert will say "Heya, Trevor!". You can use any message you want, though. If your variable is "favoriteColor" and "Blue" is the value, for instance, you can have the alert say "Blue is a very nice color! "
  2. Add in more variables if you want. Try to make a one or two sentence biography.
3.7 ★ | 3 Vote

May be interested

  • How to Turn on JavaScriptPhoto of How to Turn on JavaScript
    javascript is a language used in many websites to provide additional functionality and access to the user. developers use javascript to deliver a large part of their site's functionality to you, the user. your browser must have javascript...
  • How to Make a Basic JavaScript QuizPhoto of How to Make a Basic JavaScript Quiz
    making a game with javascript can be fun and satisfying as well as a bit of a puzzle. the code in the this article is one way of making a game using javascript. once you know the basics, feel free to adapt and play around with it. set up...
  • How to Make a JavaScript Image RolloverPhoto of How to Make a JavaScript Image Rollover
    javascript is the most popular lightweight scripting language which works in the major browsers such as internet explorer, chrome, safari, firefox, and opera. it is also easy to use to build a dynamic and interactive website. one of its...
  • How to Create a Javascript Console in Sublime TextPhoto of How to Create a Javascript Console in Sublime Text
    javascript consoles are very handy for debugging and getting live results from your script. although sublime text comes with build systems for many other scripting languages, it does not come with a built-in javascript build system. many...
  • How to Enable WebglPhoto of How to Enable Webgl
    this tipsmake teaches you how to enable webgl. webgl (web graphics library) is a javascript api that is used to render 2d and 3d graphics within a compatible web browser. open google chrome . it has an icon that resembles a red, yellow, and...
  • How to Use JavaScript InjectionsPhoto of How to Use JavaScript Injections
    javascript injection is a process by which we can insert and use our own javascript code in a page, either by entering the code into the address bar, or by finding an xss vulnerability in a website. note that the changes can only be seen...