Evaluating expressions and parentheses in JavaScript [duplicate] - javascript

This question already has answers here:
How is division before multiplication in Javascript?
(2 answers)
Closed 4 years ago.
I'm having trouble understanding how JS is processing my expressions..
This evaluates incorrectly and drops the denominator:
var a = document.getElementById("Acceleration").value;
var u = document.getElementById("Initial_Velocity").value;
var v = document.getElementById("Final_Velocity").value;
document.getElementById("Distance").value = (v*v-u*u)/2*a;
This evaluates properly:
document.getElementById("Distance").value = (v*v-u*u)/(2*a);
I'm using Dreamweaver. Thanks.

Well the formulas are different. You left out the parentheses (2*a) in the first code sample.

Related

Why in JS a.test = 1 when "a" is a number doesn't throw an error [duplicate]

This question already has answers here:
How is almost everything in Javascript an object?
(6 answers)
Closed 5 months ago.
I tested this today and I don't understand the behaviour. For me, that should be an error.
const a = 1;
a.test = 1;
console.log(a)
console.log(a.test)
Someone have the answer ?
you can define properties pretty much anything, but in this case you are assigning it to a number which doesn't actually hold properties.

Jquery: variable declared and get variable after split [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
jQuery : how to access the name of the element [duplicate]
(3 answers)
Closed 1 year ago.
I have 2 variables.
var a = 1;
var b = 2;
Now I have 2 input boxes with name 'form-1-a' and 'form-1-b'.
I need to put variables in these input boxes. Please look at below code as it is not working.
$("input[name^='form-']").val(eval($(this).attr('name').split('-')[2]));
Though entire code is bit complex but simplified and it does not seem to work without any error message.
Javascript is fun and pain at same time.
Below code worked.
$("input[name^='form-']").each(function(){
$(this).val(eval($(this).attr('name').split('-')[2]))
});

Extra parameters in a self executing function? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 1 year ago.
I saw this piece of code in a webpack compiled file
(1,2, function(x) {
console.log(x);
return 4;
})(5);
This seems to execute correctly. I am aware the 5 is a parameter to function(x)
I don't understand what the 1,2 are? How is this valid?
Well, it's because the brackets only returns the function
(thing1,thing2,thing3,thing4....,thingN) would show the last thing on the list(in this case, thingN)
Example
var bracketTest1=(1,3,5,7,9,null)
console.log(bracketTest1) //null
var bracketTest2=(null,1,2,3,4)
console.log(bracketTest2) //4
var bracketTest3=(null,1,2,4,5,function(x){return x})
console.log(bracketTest3) //function(x)
console.log(bracketTest3(Object)) //function Object()
console.log((1,2,3,4,null,undefined)) //prints undefined

Calling javascript function using dot operator [duplicate]

This question already has answers here:
How to observe value changes in JS variables
(3 answers)
Closed 5 years ago.
function getPercCalculated(x){
return (x*9.3)/100;
}
var x = 10;
var perx = getPercCalculated(x);
Instead of this I would like to call the getPercCalculated using dot operator like
var perx = x.getPercCalculated()
Can someone help me..!!
Number.prototype.getPercCalculated= function(){
return (this*9.3)/100;
};
This will attach the getPercCalculated to every number in your code tough

Javascript - Why use a double negation [duplicate]

This question already has answers here:
Why use if (!!err)?
(3 answers)
Closed 8 years ago.
Within an if condidition, when does it make sense to use a double negation? I found this pattern several times:
JS
if(!!children.length) { ...}
It's a common method to turn a normal expression to a boolean. It doesn't really make any sense inside an if statement, since it evaluates the given expression as a boolean anyways, but is useful outside.
For example:
const isEmpty = !!children.length;

Categories

Resources