Skip to content Skip to sidebar Skip to footer

Why Do People Use Variables In Some Cases?

I know this question title looks scary, but it isn't. Sorry! Ok, so, what's the point of creating a one-time-only/unchangeable 'variable'? Lets say I have one property called 'name

Solution 1:

There are any number of reasons

  1. A 'const` variable will prevent the value from accidentally changing, e.g. if you have written a closure that includes a variable that is accessible outside immediate scope.

  2. Javascript engines are not yet capable of common subexpression elimination, which is a very common compiler optimization. Using a temporary variable could improve performance, like in this example.

  3. Sometimes a variable with a different name can clarify the functionality; see Writing Really Obvious Code (ROC). Example:

    var activeUsername = User.name;  //It's not just a user's name, it's the active user's name!
  4. Sometimes that extra variable makes it easier to debug/watch variables

  5. Sometimes you want to set a separate breakpoint during the assignment

Am I overracting?

Yes.

Solution 2:

That sort of approach is most likely intended to cache a value to prevent having to do a lookup. While it generally isn't an issue, using dot notation or indexing an array value does incur a tiny bit of overhead.

The developer may have chosen to do this to clarify part of their code.

Finally, the developer may just sloppy.

In all cases, the possible benefits or side effects all depend on which JavaScript engine you're using and what types of optimizations it does.

Solution 3:

I guess I got your question. The value is stored in variable instead of using directly is because it prevents multiple roundtrips to the database or it saves time consumed in resolving the expression in case its used multiple times. for eg. in your case, Compiler would prefer to read Person.name from a variable if more than one refrences are there in your code rather than resolving the Person.name each time.

Solution 4:

I personally don't do this. But as you asked, this is what i think.

constPerson = {
    name: 'Luis Felipe Zaguini'
};
const personName = Person.name;
personName = personName + " rick"; //As in they want to make some changes to the name but they don't want to change the main object i.e Personconsole.log(`I've written my name, and it is ${personName}.`);

I personally prefer to do this. Here, if you want to make changes to the name, you have to change the main object directly. Which is bit risky as you might be using it somewhere else in the code.

constPerson = {
    name: 'Luis Felipe Zaguini'
};
console.log(`I've written my name, and it is ${Person.name}.`);

Solution 5:

friend, here you are confusing yourself between an object property and a variable. For eg, name= "foo" has no significance and can be used directly. but Person.name="foo" signifies a Person having name property value as foo.C# is all about objects and memory can always be released in finally code block. Hope I am clear :)

Post a Comment for "Why Do People Use Variables In Some Cases?"