Why isn't this variable raising when a function is executed? [duplicate] - javascript

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
What is the difference between the `=` and `==` operators and what is `===`? (Single, double, and triple equals)
(5 answers)
Closed 11 months ago.
So I'm making an idle game and I want when you buy a 'carrot patch' in this example, the price of said 'carrot patch' will raise by 50%. However this doesn't happen.
Here's my code:
var patchPrice = 10;
function buyPatch() {
if (affordable = true) {
var patchPrice = patchPrice * 1.5;
}
}

you have declared the patchPrice variable twice. so it will overwrite the previous value. just remove the second var and it will work.

These were problems with js before es6 I recommend you to use es6 const and let to declare variables.

Try == in the comparison or simply omit the (== true) as it is already a boolean.
var patchPrice = 10;
function buyPatch() {
if (affordable == true) {
var patchPrice = patchPrice * 1.5;
}
}

Avoid using "VAR" unless you wanna work with function scope variables.

Related

How JS works for multiple variables with same name? [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 2 years ago.
Why there is no Syntax error in the following code?
var num = 8;
var num = 10;
console.log(num) // 10
whereas
var num = 8;
let num = 10;
console.log(num) // already decleared error
The first case will be rendered as:
var num;
num = 8;
num = 10;
console.log(num); // output : 10
The second case will be rendered as:
var num;
num = 8;
let num; // throws Uncaught SyntaxError: Identifier 'num' has already been declared
num = 10
console.log(num);
Because unlike var, variables cannot be re-declared using let, trying to do that will throw a Syntax error: Identifier has already been declared.
What is important if you will avoid anti-patterns like global variables, if you will keep your vars in the scope and make the methods small, you will avoid redeclaration of the vars bugs.. probably.
What is important number two: var declaration works faster then const and let, so in loops, if you HAVE TO OPTIMIZE the method you can use var instead of let/const.

How to multiply objects by specified rules? [duplicate]

This question already has answers here:
Javascript: operator overloading
(9 answers)
Closed 4 years ago.
Let's say i want an object that when multiplied with, it multiplies by two and subtracts 1.
The syntax would look like this:
var a = {
on_multiply: function(context){
return context*2-1
}
};
alert(2*a);
This would output 3.
I don't want to write
"a.on_multiply(2)"
Is there a way to do this?
If yes, is it possible to do this with arrays or matrixes also?
The simplest way I can think of to make the above example work is to assign a function named a, and have the context as a parameter of that function:
function a(context) {
return (context * 2) - 1;
}
And if you really wanted a to be a function assigned to a name:
const a = context => 2 * context - 1;
And the above in ES5 syntax:
const a = functipn(context) {
return (context * 2) - 1;
}
Hopefully this helps!

What is the difference between using const and function in javascript? [duplicate]

This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 4 years ago.
What is the difference between these two functions?
const square = (number) => {
return number * number;
};
function square (number) {
return number * number;
}
There are several.
First, const prevents reassignment of the name square while function does not. Second, using an arrow function doesn't have it's own lexical context, so it won't have a scoped this and can't be used as a constructor. For reference, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Note you can also do:
const square = function(num) { return num * num }
Which both prevents reassignment and creates a lexical context.

Advantages of using let over var within a function [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 6 years ago.
Say I have a piece of code like this:
const number = 3;
function fooFunction() {
let numberTwo = 5;
var answer = number + numberTwo;
return answer;
}
finalAnswer = fooFunction();
console.log(finalAnswer);
Assuming an ES2015 compatible browser, what would be the advantages/disadvantages of using the above code, over:
const number = 3;
function fooFunction() {
var numberTwo = 5;
var answer = number + numberTwo;
return answer;
}
finalAnswer = fooFunction();
console.log(finalAnswer);
Are there any advantages or disadvantages, given they both return the same number?
As others have mentioned, in your example you can use let and var interchangeably. The difference is that let is block-scoped and var is not.
For example with var you can do this (prints 'foo'):
function printFoo(param) {
if (param) {
var x = "foo";
}
console.log(x);
}
printFoo("hello");
You cannot do this with let because let x is scoped to the if block and therefore is not defined.
Within the code sample you've provided, the two are interchangeable. Where let comes in handy is in limiting the scope to block-level as opposed to function-level. Using let inside of a for loop, for example.
It's worth noting that variables created with the var keyword are hoisted, whereas variables created with the let keyword are not.
There is an excellent breakdown of this topic by Kyle Simpson on David Walsh's blog: https://davidwalsh.name/for-and-against-let#implicit-hazards

Javascript Variable Declaration with || [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?
(8 answers)
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 8 years ago.
I'm investigating Three.js at the moment and have come across this variable declaration at the top of the main source file:
var THREE = THREE || { REVISION: '52' };
I'm just wondering what the OR (||) is doing in there - what is the function of it?
The above means:
If the value of THREE evaluates to true, assign the value of THREE to the THREE variable, otherwise initialize it to the object { REVISION: '52' }.
In code, it's like saying:
var THREE;
if (THREE) {
THREE = { REVISION: '52' };
}
else {
THREE = THREE;
}
Or:
var THREE = (THREE) ? { REVISION: '52' } : THREE;
lazy instantiation. If the variable is declared already, then assign a value to it.

Categories

Resources