?
Solved

Javascript converting "null" to string

Posted on 2009-05-07
8
Medium Priority
?
4,953 Views
Last Modified: 2012-05-06
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
Comment
Question by:tjgquicken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:secondeff
ID: 24326913
Hi,
What happens if you try using '' instead of null? {a: 'string' , b: ''}
0
 
LVL 12

Expert Comment

by:alien109
ID: 24326984
document.writeln(o.a + ((o.b)?o.b:""));
0
 
LVL 12

Expert Comment

by:Gibu George
ID: 24326994
var o = { a: 'string', b: null };
document.write((o.a==null)?'':o.a+(o.b==null)?'':o.b)
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 10

Accepted Solution

by:
bugada earned 500 total points
ID: 24327084
var o = { a: 'a', b: null };
document.write((o.a || '') + (o.b || ''));
0
 

Author Comment

by:tjgquicken
ID: 24327686
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
 

Author Comment

by:tjgquicken
ID: 24327744
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
 
LVL 10

Expert Comment

by:bugada
ID: 24327837
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
 

Author Comment

by:tjgquicken
ID: 24327876
Ok, that makes a lot more sense.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question