+ 1
why when i try to get the square root...?
When I tried to get the square root of a 10 in JavaScript I put in my code: let x = 10; console.log(x ** 1/2); when I run the program it says that is equal to 5, but when I write this: let x = 10; let y = 1/2; console.log (x ** y); It shows the really square root (3.1622) Is there a problem with my code? why does it shows 5 in the first code? thank you;
3 Answers
+ 8
x**(1/2)
** has a higher operator precedence than /
+ 6
In addition of what Lisa said, i:ll put the example of what you're doing wrong.
x**1/2
** Goes before /
So the result is x/2
If x is 9, the result will be 4.5
x**(1/2)
(Remember that square root is ** by 1/2)
so the result is âx
So if x is 9, the result is 3.0
Hope that this helps you to understand what you did wrong
+ 1
let x = 10;
console.log(x ** (1/2));