1
class Person {
  constructor(name, address, phone) {
    this.name = name;
    this.address = address;
    this.phone = phone;
  }

  call() {
    console.log(`Calling ${this.phone}...`)
  }
}

const tom = new Person('Tom', 'Uden', 0619616);
tom.call();

When calling this, the console only shows "619616". When I add console.log(tom.phone) it does log the entire number. Why is the zero excluded when calling a method?

Tom
  • 23
  • 4
  • 2
    Because noone is mentioning it: this is non-strict code, and things will get funny, when you remove the nine. – ASDFGerte Dec 13 '19 at 18:47
  • @ASDFGerte I tried this, never knew my phone number was so interesting. Also, very confused now. Thanks! – Tom Dec 13 '19 at 18:49
  • 2
    just remember "use strict"; unless you know very well what you are doing. In this case, a leading zero is a legacy notation for octal numbers. Your code would even throw in strict mode. This is just a side note though, the main problem has been described in answers and related comments. – ASDFGerte Dec 13 '19 at 18:51

4 Answers4

5

You are using it as integer.

If you want to keep zero's, use it as string.

class Person {
  constructor(name, address, phone) {
    this.name = name;
    this.address = address;
    this.phone = phone;
  }

  call() {
    console.log(`Calling ${this.phone}...`)
  }
}

const tom = new Person('Tom', 'Uden', '0619616');
tom.call();

For explanation, check comments or this link

Another good to read reference: https://stackoverflow.com/a/37004175/10473393 (Thanks for @Amy for sharing in comments)

Alexander Santos
  • 1,458
  • 11
  • 22
  • 1
    Thanks for this solution. Could you also explain why the zeros disappear when using an integer? Has it something to do with JavaScript reading it as 'false' and thus null? – Tom Dec 13 '19 at 18:43
  • 2
    @tomdevisser No, it just doesn't track leading zeroes because they don't have any mathematical meaning. If you tried to use, say "123.00" as a number, it would remove the trailing zeroes for the same reason. If you want to preserve formatting rather than just the value of the number, you have to make it a string. – John Montgomery Dec 13 '19 at 18:46
  • 2
    @tomdevisser `01234` is the same number as `1234`. They have the same, literal representation in binary and are not distinguished. The only way to distinguish them is to force the leading zeroes to be stored: using a string. –  Dec 13 '19 at 18:47
  • 2
    @tomdevisser I think I should add one caveat to what I said. In JS (this may apply to other languages), numbers with a leading zero can be considered *octal* by mistake. See https://stackoverflow.com/questions/37003770/why-javascript-treats-a-number-as-octal-if-it-has-a-leading-zero –  Dec 13 '19 at 18:49
  • 1
    I added Amy's link to the reply, together with another reply that explains basically the same as you guys commented, just to help future Googlers, as a matter of reference. – Alexander Santos Dec 13 '19 at 18:53
  • 3
    @tom If I can add one more thing, while a phone number is a "number", its not the kind of number you're going to use in math. So, while it's a "number", it's not a "number", if you get my meaning. It's technically valid to have a phone number with leading zeroes, which need to be retained, and since you aren't going to use math on a phone number, perhaps a string is a more appropriate way to store it. The same logic applies to zip codes, street numbers, and other "numbers" that are never "math'd". –  Dec 13 '19 at 19:02
  • @Amy Thanks for adding this, that's a good rule of thumb to keep in mind. :) – Tom Dec 13 '19 at 19:09
2

You can convert it to string, and pad start the leading zeros. For example, I assume you need a 7 digit phone number.

class Person {
  constructor(name, address, phone) {
    this.name = name;
    this.address = address;
    this.phone = phone;
  }

  call() {
    console.log(`Calling ${this.phone.toString().padStart(7, 0)}...`)
  }
}

const tom = new Person('Tom', 'Uden', 0619616);
tom.call();
Han Xiao
  • 46
  • 2
1

The value what you pass in a Number type. If you would like to keep the whole value as is then you need to use String, just like the following:

const valueNumber = 0619616;
const valueString = '0619616';

console.log({valueNumber, valueString});

I hope that helps!

norbitrial
  • 14,716
  • 7
  • 32
  • 59
  • Thanks! I'm only trying to understand why this is necessary, is there a reason the zero doesn't log? – Tom Dec 13 '19 at 18:45
  • 2
    @tomdevisser Per my other comment, that leading zero has no representation and cannot be distinguished from the numbers "actual value" –  Dec 13 '19 at 18:50
1

to include zero in left side you should use it as string

  constructor(name, address, phone) {
    this.name = name;
    this.address = address;
    this.phone = phone;
  }

  call() {
    console.log(`Calling ${this.phone}...`)
  }
}

const tom = new Person('Tom', 'Uden', '0619616');
tom.call();

mohamed ali
  • 202
  • 2
  • 3