We cannot use 1.toString(), but we can use `let a = 1; a.toString()`; Why? [duplicate] - javascript

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Calling member function of number literal
(3 answers)
Closed 2 years ago.
Is this means assignment in js of primitive makes primitive become object automatically?
let a = 1 just be transferred to let a = new Number(1), we know Number is a function, and it's prototype has toString, this makes sense? Is it right?
Finally, we know the primitive of js is stored in stack memory, but if we can only get object by assignment, so is it means only pointer exists in stack? I am confused. Thanks for your answers if you can help me.

You can not call Number.prototype methods directly from a number in digits form, e.g. 1 2 3 4 5 6 7 8 9, you have to wrap them in parentheses.
// This doesn't work
console.log(1.toString());
// This works
console.log((1).toString());

Related

How does "a^=b; b^=a; a^=b;" swap the values of the variables [duplicate]

This question already has answers here:
Swapping two variable value without using third variable
(31 answers)
How does XOR variable swapping work?
(11 answers)
Closed 10 months ago.
I found out a neat trick to swap the values of two variables without having to create an auxiliar variable. I'll use javascript to illustrate it, although I'm guessing it's language agnostic.
let a = 1;
let b = 2;
a^=b; b^=a; a^=b;
console.log(a); // Prints 2
console.log(b); // Prints 1
Can someone explain how does the swap happen? What does ^= do?

Is it possible to cover a variable with a class JavaScript? [duplicate]

This question already has answers here:
Creating simple constructor in Javascript
(3 answers)
javascript - how to get an object to return a value that is not the object itself
(4 answers)
Closed 2 years ago.
Suppose we have a class called Foo() which takes the value as an argument and has some method method1, metohd2,....
let y = new Foo(56);
console.log(y) // output should be 56 not the class object
y.addOne() // output: adds one to the value
console.log(y) // output should be 57
Can we do this? Not especially with Numbers only, but with other datatypes too?

What does ‘...’ in JavaScript do? [duplicate]

This question already has answers here:
What "..." means in Javascript (ES6)? [duplicate]
(1 answer)
Spread Syntax vs Rest Parameter in ES2015 / ES6
(11 answers)
Closed 4 years ago.
I’m new to coding and slef teaching JavaScript.
My task I was set was to identify the largest value in an array.
My soloition works, but I needed the ‘...’ for it to work properly.
My question is, what does the ‘...’ in the last line actually mean/do?
function createAnArr(){
let myArr = prompt("Enter your numbers separated by commas:").split(",");
return myArr;
}
console.log(Math.max(...createAnArr()));
'...' it means it will be able to take any number of arguments of the respective scope
'...': The spread operator is used for array construction and destructuring, and to fill function arguments from an array on invocation. A case when the operator spreads the array (or iterable object) elements.
more details you can see here

why 2.valueOf() is not valid but (2).valueOf() is? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 4 years ago.
I guess that javascript will parse (2).valueOf() to new Number(2).valueOf() but why it doesn't for the first one ?
According to the operator precedence, the grouping operator shall have a higher priority than the member access https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
So why (2) is not be evaluated first and yield 2 instead of be parsed to new Number(2) ?
Because in 2.valueOf the . is considered to be as a part of 2 instead of being understood as method accessing.
That is why 2..valueOf() works.
console.log(2..valueOf());

Why can't I do 1.toString() but I can do var a = 1; a.toString() [duplicate]

This question already has answers here:
Usage of toString in JavaScript [duplicate]
(3 answers)
Closed 6 years ago.
When I do
1.toSting()
I get an error, but
// javascript
var a = 1;
// or c#
int a = 1
a.toString()
works. Why is it that when a number gets assigned to a variable, it get some special functions?
The . is interpreted as you want a decimal/floating-point literal, not invoking a member.
You can do this in JavaScript
// Option 1
(1).toString();
// Option 2
1.0.toString();
// Option 3
1..toString();
In C#, it appears your only option is (1).ToString(), but the lexer might be smart enough not to need them.

Categories