For starters: this is NOT how you should declare a variable in javascript:
attackValue = $(this).text();
This would clutter 'programs' variable space. You should 'always' declare a variable in javascript using the var keyword.
When I want to use attackValue outside of the click() It will not be defined.
It depends on where and when you want to use it. Consider the following code:
var attackValue = 'some value';
function setValue() {
attackvalue = 'some other value';
}
console.log(attackvalue); // this would output some value, because the function hasn;t run yet
setValue();
However if you would do:
var attackValue = 'some value';
function setValue() {
attackValue = 'some other value';
}
setValue();
console.log(attackValue); // this would output some other value, because the function did already run
Note that the above code should run in a closure. Or it would still clutter the variable space (even when using the var) keyword. So it would look more like:
(function() { // yay closure, now all variables defined in here will stay in here
var attackValue = 'some value';
function setValue() {
attackValue = 'some other value';
}
setValue();
console.log(attackValue); // this would output some other value, because the function did already run
})();