Tsukimoto Mitsumasa
Tsukimoto Mitsumasa

Reputation: 582

Variable inside window.confirm

I have this piece of code:

use_result = window.confirm("You have 1 registration credits. Do you wish to use a registration credit for this player?");

The digit 1 there is not dynamic, it is manually assigned. What I want is that I will have a variable placed instead of the digit 1, such that it's value is dynamic. Would that be possible? Thanks.

Upvotes: 0

Views: 2240

Answers (3)

Lix
Lix

Reputation: 47956

You can simply replace that static value with a variable -

var numberOfRegistrations = 1;
use_result = window.confirm("You have "+ numberOfRegistrations +" registration credits. Do you wish to use a registration credit for this player?");

The final string that is displayed in the confirm dialog will be a concatenation of the surrounding strings and your predefined dynamic variable.

Here is a very simple jsFiddle demonstration


In the same way that when you add numeric values together you will get the sum of those values, with strings, using the + operator will simple combine the two strings.

var firstWord = "Stack";
var secondWord = "Overflow";

alert(firstWord + seconfWord); // This will alert the string - "StackOverflow";

You can combine as many strings as you want together to create a final string -

alert('Hello' + ' ' + 'World'); // This will alert the string - "Hello World"

Note in the last example I am joining 3 strings.

  • Hello
  • A blank space
  • World

Upvotes: 1

Jacob Sharf
Jacob Sharf

Reputation: 250

use string concatenation with the + operator.

var a = /*... */;
use_result = window.confirm("You have "+ a +" registration credits. Do you wish to use a registration credit for this player?");

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160181

Yes; the easiest would use string concatenation:

use_result = window.confirm("You have " + n + " registration credits. Do you wish to use a registration credit for this player?");

Upvotes: 4

Related Questions