Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace undefined with a empty string

Tags:

I am using jsPdf. When a field has been left blank "undefined" is printed on the pdf. I would like to replace that with a empty string. I am trying to use a if statement but I am not getting it.

 doc.text(30, 190, "Budget : $");
    if ($scope.currentItem.JobOriginalBudget == "undefined") {

        doc.text(50, 190, " ");
    }
    else {
        var y = '' + $scope.currentItem.JobOriginalBudget;
        doc.text(50, 190, y);
    };
like image 965
texas697 Avatar asked Sep 16 '14 18:09

texas697


People also ask

Is undefined equal to empty string?

undefined is == only to null , and not to all other "falsy" values: 0. "" - empty string.

Is an empty string null or undefined?

null. undefined (value of undefined is not the same as a parameter that was never defined) 0. "" (empty string)

Is empty string considered undefined in JavaScript?

There are only six falsey values in JavaScript: undefined , null , NaN , 0 , "" (empty string), and false of course.

How do you check if a string is undefined?

In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.


2 Answers

As per this answer I believe what you want is

doc.text(50, 190, $scope.currentItem.JobOriginalBudget || " ")
like image 74
Nuno Costa Avatar answered Sep 18 '22 22:09

Nuno Costa


undefined is a primitive value. Instead of comparing against the identifier undefined, you're comparing against the 9-character string "undefined".

Simply remove the quotes:

if ($scope.currentItem.JobOriginalBudget == undefined)

Or compare against the typeof result, which is a string:

if (typeof $scope.currentItem.JobOriginalBudget == "undefined")
like image 37
apsillers Avatar answered Sep 19 '22 22:09

apsillers