alert a variable value

technocrat picture technocrat · Jun 6, 2010 · Viewed 289.4k times · Source

How do I display the value of a variable in javascript in an alert box?

For example I've got a variable x=100 and alert(x) isn't working.

the script used in grease monkey is here

var inputs = document.getElementsByTagName('input');

var new;
for (i=0; i<inputs.length; i++) {
  if (inputs[i].getAttribute("name") == "ans") {   
new=inputs[i].getAttribute("value"));

alert(new)

  }
}

Answer

JosephDoggie picture JosephDoggie · Jan 7, 2016

Note, while the above answers are correct, if you want, you can do something like:

alert("The variable named x1 has value:  " + x1);