5

Let's look at the examples below:

Q1: Why is the output 0 here? What does it mean?

var a = 7;
console.log(a.constructor()); // prints 0 (Why?)

Q2: When typeof a and typeof 7 both are number, why a.constructor() runs whereas 7.constructor() doesn't?

var a = 7; 
var bool = typeof a === typeof 7;

console.log(a.constructor()); // 0
console.log((++a).constructor()); // 0

console.log(7.constructor()); // SyntaxError: Invalid or unexpected token
console.log(++a.constructor()); // ReferenceError: Invalid left-hand side expression in prefix operation
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
Aaditya Sharma
  • 3,330
  • 3
  • 24
  • 36
  • 1
    `7.whatever` is simply incorrect syntax because the parser expects a numeric literal, .e.g. `7.123`. But you can go around it with `(7).whatever` – VLAZ May 13 '19 at 13:59
  • @VLAZ So, what is the difference between `(7).constructor()` and `7..constructor()`? Are they same? – Aaditya Sharma May 13 '19 at 14:03
  • @AadityaSharma the same. They'd avoid the dot being parsed as part of the numeric literal and just get `constructor` from the Number representation of the primitive and execute it. – VLAZ May 13 '19 at 14:04
  • 1
    Possible duplicate of [Why can't I access a property of an integer with a single dot?](https://stackoverflow.com/questions/9380077/why-cant-i-access-a-property-of-an-integer-with-a-single-dot) – adiga May 13 '19 at 14:08
  • @adiga good dupe. Unfortunately can't currently vote for it. Annoyingly, it only answers *half* the question because this question is...two questions. It's probably also Too Broad as the questions aren't really too much related. – VLAZ May 13 '19 at 14:10
  • Please avoid asking multiple questions in a single post – adiga May 13 '19 at 14:12
  • @adiga: The thread is useful to this discussion. However, it answers only the 2nd half of the question. – Aaditya Sharma May 13 '19 at 14:12
  • 1
    There shouldn't be a "*2nd half of the question*" [One post with multiple questions or multiple posts?](https://meta.stackexchange.com/questions/39223) – adiga May 13 '19 at 14:17

3 Answers3

2

Q1: Why is the output 0 here? What does it mean?

a.constructor is Number and you are calling it with first argument undefined. Because Number() returns undefined so x.constructor() returns undefined. If no argument is passed to Number() it returns 0

var a = 5;
console.log(a.constructor === Number)
console.log(Number())

When typeof a and typeof 7 both are number, why a.constructor() runs whereas 7.constructor() doesn't?

Actually 7. is itself a number. Here . doesnot work as Dot Notation but as decimal point because the digits after decimal point are optional.

Solution:

There can be different ways to directly access the method of number.

console.log(5..constructor)
console.log((5).constructor)
console.log(5 .constructor)
Community
  • 1
  • 1
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
1

Q1: Because js number is object and has method constructor (which returns 0 when no parameter is provided)

Q2: Because first dot is interpret by js as decimal dot, but try

console.log( 7..constructor() );
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
1

Q1: Why is the output 0 here? What does it mean?

The constructor method is there to determine the type of the variable (Check the example)

var a = 7;
console.log(a.constructor == Number);

var b = new Object;
console.log(b.constructor == Number);
console.log(b.constructor == Object);

Q2: When typeof a and typeof 7 both are number, why a.constructor() runs whereas 7.constructor() doesn't?

Because the point after 7 is treated like a decimal and therefore it gives an error because constructor() is not a number. The code below would fix this (even tho it makes no sense):

console.log((7).constructor())
Community
  • 1
  • 1
MauriceNino
  • 6,214
  • 1
  • 23
  • 60