0

I am using ng-repeat in angular js, I am having an add button where I need to just add an empty object so can i initialize variable as undefined instead of empty.

ex my HTML

<tr ng-repeat="test in tests">
 <td>.....</td>
</tr>

Controller

function getStestItem() {
    var testItem = {
        ItemID: null,
        Test: "",
        Description: ""
    }
    $scope.tests.push(skillItem);
}
$scope.add = function(){getStestItem();}

so instead of giving empty values can I give as

function getStestItem() {
    var testItem = {
        ItemID: null,
        Test: undefined,
        Description: undefined
    }
    $scope.tests.push(skillItem);
}

Please suggest.

Rahal Kanishka
  • 676
  • 10
  • 26
Sultan
  • 123
  • 1
  • 17
  • 1
    Don't set things to `undefined`. Reserve that value to mean "not yet set to anything". Set it to `null` instead. – Mike Eason May 05 '16 at 10:08
  • Did u try to give them null values instead of "undefined"? – Rahal Kanishka May 05 '16 at 10:09
  • I Tried giving undefined, the reason i was asking for undefined is as the model scope will be undefined initially. – Sultan May 05 '16 at 10:13
  • By definition, variables that aren't initialized are always undefined. [Read more here](http://stackoverflow.com/a/28825847/4046274) – Pirate X May 05 '16 at 10:14
  • its same as u set the value of ItemID to "null", for more details check this out : http://stackoverflow.com/questions/5101948/javascript-checking-for-null-vs-undefined-and-difference-between-and – Rahal Kanishka May 05 '16 at 10:14
  • @PirateX so is it fine giving undefined?? – Sultan May 05 '16 at 10:16
  • @Sultan Essentially null and undefined mean the same thing even though they are different values. It all depends on the convention of how you use it in your system. – Pirate X May 05 '16 at 10:19
  • @PirateX Thanks for your response, only question can the variables in angular js be initialized with undefined? Is it write way or wrong in doing so? – Sultan May 05 '16 at 10:21
  • it's not really clear what you are trying to accomplish here. Setting a value to `undefined` is the same as not supplying it at all. Since JavaScript objects are dynamic and properties can be added and removed at any time, providing properties that are `undefined` is redundant. – Claies May 05 '16 at 10:22
  • @Sultan Use `NULL` because it is now the trend/industry adopted standards. However I personally prefer to let things be undefined. – Pirate X May 05 '16 at 10:27
  • @Claies: I am adding an empty model on click of add button using ng-repeat, my only question is while I am trying to add an model, so i am asking what is the best practice to add a model from controller, to give empty value or keep it as undefined? – Sultan May 05 '16 at 10:31
  • @PirateX: Thanks, that's what i was also preferring just wanted to know what is the best practice. – Sultan May 05 '16 at 10:32
  • Yes, it's clear you are adding an empty model. That's the point. You don't need to add things that don't exist, it doesn't do anything. – Claies May 05 '16 at 10:32
  • @PirateX you can add flag if it is an question that helps other as well. – Sultan May 05 '16 at 10:33
  • Then how would i add gimme an example . @Claies – Sultan May 05 '16 at 10:34
  • what you have is **identical** to if you did `var testItem = {}`. There is almost never a reason to add properties to the object if they don't have a value; the property will be dynamically added to the object when a value is assigned. – Claies May 05 '16 at 10:37

1 Answers1

0

This will be too much for a comment, So I will leave it as a answer here

null is slightly more specific than undefined: It's a blank object reference. JavaScript is loosely typed, of course, but not all of the things JavaScript interacts with are loosely typed.

If an API like the DOM in browsers needs an object reference that's blank, we use null, not undefined.

And similarly, the DOM's getElementById operation returns an object reference — either a valid one (if it found the DOM element), or null (if it didn't).

This answer is not a mine, for more details click here
Hope this helps

Community
  • 1
  • 1
Rahal Kanishka
  • 676
  • 10
  • 26