Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set variable inside val

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);
like image 358
northamerican Avatar asked Sep 09 '12 22:09

northamerican


People also ask

What is var {} in JS?

The var statement declares a variable. Variables are containers for storing information. Creating a variable in JavaScript is called "declaring" a variable: var carName; After the declaration, the variable is empty (it has no value).

Can you change a global variable in a function?

Functions can access global variables and modify them. Modifying global variables in a function is considered poor programming practice. It is better to send a variable in as a parameter (or have it be returned in the 'return' statement).

How do you change the value of a variable?

Changing the Values of Variables. You can change the value of any variable or the contents of any memory location displayed in a Variable Window, Expression List Window, or Stack Frame Pane by selecting the value and typing the new value. In addition to typing a value, you can also type an expression.

How do you assign a variable in JavaScript?

To create a variable in JavaScript, use the let keyword. To be concise, we can combine the variable declaration and assignment into a single line: let message = 'Hello!'; // define the variable and assign the value alert(message); // Hello!


2 Answers

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.

like image 179
Dave Newton Avatar answered Oct 15 '22 21:10

Dave Newton


I'll entertain the idea such a construct could be acceptable in certain cases but not this one especially since there are other stylistic issues in the given example (the biggest one being "where does the magic number come from")

Main concern IMO is whether or not the variable has been declared - you can't simply

$('#price').val(var default_price = 2.9); //nope

and if the original code is used with the variable not yet declared, you end up polluting the global scope. However, if the variable has been declared, it brings up a follow-up question "why has it not been declared with the correct default value". Alternatively, the magic number could be different depending on an (unknown) condition:

if (/*whatever*/) {
  $('#price').val(default_price = 2.9);
} else {
  $('#price').val(default_price = 9522); //over 9000
}

Again, this is stylistically poor since setting the value of #price should have been executed outside of the conditional (or a switch statment):

if (/*whatever*/) {
  default_price = 2.9;
} else {
  default_price = 9522;
}
$('#price').val(default_price);

There could be a convoluted case where the variable setter is overridden to return something other than the value assigned which is a bit of a questionable practice in the first place IMO.

like image 29
Oleg Avatar answered Oct 15 '22 21:10

Oleg