This appears to work and is valid, but is there any reason I shouldn’t do this? It saves me a line of code and lets me set a variable and the value of a text area.
$('#price').val(default_price = 2.9);
It’s equivalent to this:
default_price = 2.9;
$('#price').val(default_price);
It embeds code doing one thing inside code doing something entirely different.
Particularly if you’re talking about default values, “constants”, etc., conflating initialization with UI interaction leads to confusion. Keep them separate–easier to find and maintain.
Technically it’s the same thing. Cognitively it isn’t.
o.v. raises the specter of global namespace pollution. By declaring variables in arbitrary locations you increase the odds of over-writing a value, fat-fingering an identifier, duplicating effort, and so on.
In addition to creating difficult-to-isolate bugs, this is an additional cognitive load, because you then have to understand the scope of the declared variable, locate where else it may be used, etc.