0

var data = [{
 "error": false,
 "date": "09-02-2016",
 "day": "5",
 "checkin": "20:29:11",
 "checkout": null,
 "break_timein": null,
 "break_timeout": null,
 "checkin_remarks": "test",
 "checkout_remarks": null,
 "break_time_remarks": null
}];



console.log(data[0].checkin)
console.log(data[0].break_timein)
console.log(data[0].break_timein.length > 0 || data[0].break_timein != null)

For break_timein when i console.log it, it returns null but when I get the length it returns Cannot read property 'length' of null

Why is this?

Brownman Revival
  • 3,620
  • 9
  • 31
  • 69
  • 4
    its because you can not get a length of null – Özgür Ersil Sep 02 '16 at 13:01
  • @ÖzgürErsil how do you suggest to fix it to make it boolean? i want to put it inside `if(data[0].break_timein.length > 0 || data[0].break_timein != null)` – Brownman Revival Sep 02 '16 at 13:02
  • This feels like a completely self-explanatory problem when I see the error, so I think you might have to elaborate on what your mental understanding of your object is, and how it's not fitting your thinking. In regards to the comment above: You most likely want `etc != null &&`, instead of `||`. A null-check *before* checking something's value is pretty common, but you can't do it afterward, and you can't use an OR operator. – Katana314 Sep 02 '16 at 13:03
  • @Katana314 i understand that it only appears when there is not property `break_timein` but it is there its just `null` – Brownman Revival Sep 02 '16 at 13:04
  • 1
    @BrownmanRevival: `undefined` and `null` behave the same way wrt to property access: You cannot access a property on them. As already said, the error is pretty self-explanatory: *"Cannot read property 'length' of **null**"* . Lets simplify the problem: Type `null.length` and `undefined.length` in the console. – Felix Kling Sep 02 '16 at 13:06
  • @FelixKling ok will try it thanks for explanation can you add solution this explanation to answer? – Brownman Revival Sep 02 '16 at 13:09
  • @FelixKling correct me if im wrong when using `!=` it will treat `"null"` and `null` same right? so if the value of `data[0].break_timein` is `"null"` it will still be true?even if it really has value. – Brownman Revival Sep 02 '16 at 13:12
  • What do you mean by "the same"? *"if the value of data[0].break_timein is "null" it will still be true"* `"null"` is a non-empty string, so it evaluates to `true` if that's what you mean. – Felix Kling Sep 02 '16 at 13:19

2 Answers2

1

I understand that it only appears when there is not property break_timein but it is there its just null

You drew the wrong conclusion here. The error doesn't have anything to do with whether a property exists or not.

When a property doesn't exist and you are trying to access a property on it you are getting the following error:

var foo = {};
foo.bar.baz;
// Uncaught TypeError: Cannot read property 'baz' of undefined

The "undefined" in this sentence doesn't refer to the existence of the property, it refers to the value undefined. In JavaScript, when accessing a property doesn't exist, accessing it will return the value undefined:

foo.bar
// undefined

There are other situations where undefined is created, e.g. when you define a variable without an initial value:

var abc;
abc;
// undefined

Knowing that we can test what happens when we directly access a property on undefined:

undefined.foo
//  Uncaught TypeError: Cannot read property 'foo' of undefined

This throws the same error as above, so it doesn't have anything to do with property access.


So now we have established that undefined cannot be used in a property access context.

null is another value that is just like that:

null.foo
// Uncaught TypeError: Cannot read property 'foo' of null

What makes null and undefined different from other primitive values (string, number, boolean) that they show this behavior?

null and undefined are actually values of two unique data types: Null and Undefined.

Those data types do not have an object wrapper. Strings, Numbers and Boolean values can exist in two forms: as primitive values and as object value:

var primitive = "foo";
var object = new String("foo");

When you are accessing a property on a primitive value, e.g. "foo".length, JavaScript automatically converts the primitive value to a temporary object value, similar to new String("foo").length.

But since there is no object equivalent for Nulland Undefined (evident by the fact that there is no Null or Undefined function), this cannot be done, so an error is thrown.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • what i mean when i said earlier is when the value of `data[0].break_timein` is the string null. in current condition `data[0].break_timein != null` this will evaluate to true even if the value is string `null`. I hope it is clear now. – Brownman Revival Sep 02 '16 at 13:24
  • 1
    The only values for which `x == null` is `true` is `null` and `undefined`. So yes, `x != null` will be `true` for *any* value, except `null`and `undefined`. – Felix Kling Sep 02 '16 at 13:30
  • thank you for explanation im really confused with these things :) happy coding mate – Brownman Revival Sep 02 '16 at 13:31
0

Here, null doesn't have any properties eg- length that's why it's throwing an error saying "Cannot read property 'length' of null". You should write the statement like this:

console.log(data[0].break_timein != null || data[0].break_timein.length > 0);
  • This is probably minor semantics not important to the issue, but I don't think of `null` as an object. It is in Ruby, but not JavaScript - you can check the `length` property of `{}`, which is an object, but since that property isn't on the object, it will be `undefined`. On the other hand, checking any property of `null` will actually cause an error. – Katana314 Sep 02 '16 at 13:07