I have these lines in my code:
var Y = 1;
var Z = X || Y;
Where, in a some cases - X is not defined while Y holds a value.
Though I swear I thought it was working before..
I suddenly get "Uncaught ReferenceError: X is not defined".
Wasn't the || operator meant to support such cases?
It does seem to work for:
var X = X || 1;
When X was never defined before..
Is this due to some sort of JS parsing limitation? I'm curious to know.
Thanks
I suddenly get "Uncaught ReferenceError: X is not defined".
You are trying to read from a variable before you've declared it with var (or a function argument list) or written to it.
Wasn't the || operator meant to support such cases?
No.
It does seem to work for var X = X || 1;
You have a var X so the X variable is declared in that instance.
With var Z = X || Y; you are declaring Z but not X (Y was declared on the previous line).
var X = X || 1; works due to hoisting.
It's actually:
var x;
x = x || 1 // undefined || 1
Related
var shaftDepth = 1;
var nextShaftDepth = ((maxThreshold - shaftDepth) / 20) + shaftDepth;
var maxThreshold = 10;
The equation for nextShaftDepth spits out an NaN value.
For context, I'm planning to have an "upgrade" in my game which decreases the distance of "shaftDepth" to "maxThreshold" by 5%. To do this, shaftDepth = nextShaftDepth after the formula is done.
"nextShaftDepth" is expected to be equal to 1.45, but instead it just returns an NaN value. Am I doing approaching this wrong or is my syntax incorrect? Thanks.
var is hoisted, it will be undefined as you're trying to access it before the declaration , change your code to
var shaftDepth = 1;
var maxThreshold = 10;
var nextShaftDepth = ((maxThreshold - shaftDepth) / 20) + shaftDepth;
I've been following w3schools tutorials for JavaScript ES6, and after showing some example code for arrow functions:
const x = (x, y) => x * y;
They state the following.
Using const is safer than using var, because a function expression is always constant value.
I don't fully understand what they mean here. Are they referring to the fact that I can't edit the arrow function after the expression? Even if that's the case, how exactly does that make the use of const 'safer' than using var? Would something bad happen if I were to write:
var z = (x, y) => x * y;
z = x => x *= -1;
declaring z as const will make sure the function is not overridden later.
Using var will allow the function z to be overridden, line 2 in below code will update z with the function (x) => x*= -1;
var z = (x, y) => x * y; // this code will be forgotten
z = x => x *= -1; // No Error
If const is used, line 2 will give error as z value cannot be changed
const z = (x, y) => x * y; // Final expression - cannot be changed
z = x => x *= -1; // Error
Well, you won't get any errors by doing this.
var z = (x, y) => x * y;
z = x => x *= -1;
But, as you know, functions are intended to contain reusable code and a function is declared to be called later.
If you use var for function expression, that means it can be changed later and you will lose the function you declared.
You declare a function on line 1, but without using it, you declare another function on line 2 and that means the first line is not necessary at all.
Your code is equivalent to the below code block.
function not_used(x, y) {
return x * y;
}
function used(x) {
return x * -1;
}
So, why declaring a function that you don't use?
I'm going through a JavaScript book and have been executing code samples using Node.js. I've been writing files then running node file.js.
Normally, I use semicolons writing JavaScript, but I'm following the book's style.
I came across a error while executing one of the code samples, and I can't figure out why it happens.
Executing:
var x = 2
var y = 3
(x == 2) && (y == 3)
(x > 3) || (y < 3)
Results in the error:
TypeError: 3 is not a function
If I add a ; after line 2, e.g.
var x = 2
var y = 3;
(x == 2) && (y == 3)
(x > 3) || (y < 3)
It results in the error:
TypeError: (y == 3) is not a function
However, if I place a ; after the third line as well, e.g.
var x = 2
var y = 3;
(x == 2) && (y == 3);
(x > 3) || (y < 3)
Things work fine.
Or if I run each line (individually) in the Node.js command line, everything works fine.
It's probably just my misunderstanding of Node.js or JavaScript. But I couldn't find another similar situation online.
I'm using OS X 10.11.1 and Node.js v5.2.0 (installed through Homebrew).
This would happen in any JavaScript environment, not just node. There are specific rules as to when semi-colons can be omitted. This is why the majority if the community advocates against omitting semi-colons.
This is being executed as a single statement:
var y = 3
(x == 2) && (y == 3)
(x > 3) || (y < 3)
Statements are delineated by context (or semi-colon), not by whitespace or newlines.
var y = parseInt('10px');
is the same as
var y = parseInt
('10px');
is the same as
var y = parseInt ('10px');
So when you try to execute var y = 3 (x == 2), the JIT is interpreting 3 as a function, due to the parenthesis that follow it.
If the next line starts with a '(' then the statement is not terminated by a new line. Therefore, you need a ';' or some other token to specify that the statement has ended.
You can read more about javascript and semicolons here and here.
Your first error is occurring because the code is being interrupted as:
var y = 3(x == 2)
The second error if from the code being interrupted as:
(y == 3)(x > 3)
These are invalid.
Adding the semicolons changes your code to
var y = 3;(x == 2)
and
(y == 3);(x > 3)
These are valid.
Javascript uses the semicolon and not newline to denote the end of a statement and the possible beginning of another. So, when you tried to execute:
var x = 2
var y = 3
(x == 2) && (y == 3)
(x > 3) || (y < 3)
It interpreted it as:
var x = 2 var y = 3(x==2) && (y==3)(x > 3) || (y<3)
Which seemed like you were trying to initialize y with the value of function 3(x=2) which is wrong syntax and semantics for a function declaration in javascript.
When you put a semicolon after the second line, it interpreted lines 1 and 2 as you meant them to be interpreted, but again a similar issue arose in lines 3 and 4, which were fixed once you added the semicolons.
You only need semicolons where javascript cannot tell the end of one statement and the beginning of another. You can omit them, but the general rule is something like this.
As you can see here,
The source
a = b + c
(d + e).print()
is not transformed by automatic semicolon insertion, because the parenthesised expression that begins the second line can be interpreted as an argument list for a function call:
a = b +c(d + e).print()
In the circumstance that an assignment statement must begin with a left parenthesis, it is a good idea for the programmer to provide an explicit semicolon at the end of the preceding statement rather than to rely on automatic semicolon insertion.
It's a well known pitfall when coding without semicolons in JavaScript and that also behaves the same for Node.js.
I need to have two variables, x and y, and an equation involving both that always equals one.
This is a simple example. This example may be easy to define in terms of y, like y = 1/x, but I have another equation I need to use, and it is too hard to define in terms of y.
var x, y;
x*y = 1;
x = Math.random()*20;
console.log(y);
I would get an error message for this, like
Uncaught ReferenceError: Invalid left-hand side in assignment (line 2)
a variable could be defined as x*y, like var z= x*y, but apparently not a constant. Maybe there is some way around this, like defining two variables, one as the constant and the other as the equation and finding some way to relate them? Maybe javascript's looseness will allow for some new technique?
Thanks ahead of time! :)
You can't do this directly. The value of x depends on the value of y. You must describe their dependency.
You can do something like:
var x_y = (function () {
var x = 1, y = 1;
return {
set_x: function (new_x) {
x = new_x;
y = 1 / new_x;
},
set_y: function (new_y) {
y = new_y;
x = 1 / new_y;
},
get_x: function () {
return x;
},
get_y: function () {
return y;
}
};
}());
x_y.set_x(Math.random()*20);
console.log(x_y.get_y());
So essentially we're saying x = 1 / y; and y = 1 / x; and we couple the values: if you change one, the other changes as well.
I googled a lot on this and didn't get any useful results, I'm trying to declare a variable inline, in C# it works as follows:
int x, y;
x = 5 + (y = 6) + 7;
which will assign y=6 and x=18 and if used right, allows you to do some crazy things in one line.
And my question is, how to do it in JS? Is it even possible?
The only modification required is switching int to var
var x, y;
x = 5 + (y = 6) + 7;
JS Fiddle: http://jsfiddle.net/RP4Dj/