Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6873
  • Last Modified:

Javascript converting "null" to string

I'm working on a Javascript program and I'm trying to figure out the best way to do the following:
I have a string and I want to concatenate it with another variable. The problem is that if the other variable is null, what I wind up with is my first string concatenated with the word "null". Instead, I want to have my first string concatenated with an empty string. Is there any way to change the default Javascript behavior, maybe by changing the toString function or something in String.prototype, so that "casting" a null to a String results in an empty string? Thanks.
var o = { a: 'string', b: null }
document.writeln(o.a + o.b);                     // prints "stringnull", i want it to just print "string"

Open in new window

0
tjgquicken
Asked:
tjgquicken
1 Solution
 
secondeffCommented:
Hi,
What happens if you try using '' instead of null? {a: 'string' , b: ''}
0
 
alien109Commented:
document.writeln(o.a + ((o.b)?o.b:""));
0
 
Gibu GeorgeChief Technology OfficerCommented:
var o = { a: 'string', b: null };
document.write((o.a==null)?'':o.a+(o.b==null)?'':o.b)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
bugadaCommented:
var o = { a: 'a', b: null };
document.write((o.a || '') + (o.b || ''));
0
 
tjgquickenAuthor Commented:
Bugada probably has the most efficient solution, but I guess that having to check every variable for being null before I use it is just something I'm going to have to get used to in Javascript.
0
 
tjgquickenAuthor Commented:
Still I noticed the following:

new String() initializes an empty string
new String(null) initializes the character string "null"
new String(undefined) initializes the character string "undefined"
delete c; new String(c); throws a ReferenceError because c is not defined

There doesn't seem like a lot of logic to this behavior.
0
 
bugadaCommented:
No the behavior is coherent to javascript specifications.

null and undefined are reserved words with a special meaning so if you pass it in the string constructor  it creates an object with these values. This strings print their literal value as output, but you can't do normal operation on them on them, for example a substring.

The default String constructor creates an empty string.

The last line is correct because every variable used must be declared in its context, although most browsers initialize undeclared variable as undefined under the window contex. IE is very strict on this issue.
0
 
tjgquickenAuthor Commented:
Ok, that makes a lot more sense.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now