Answer by Francisco Costa for Concatenating strings with `if` statements in...
I liked the readability of Demian Brecht answer, but I would only change the string for a regex instead, because the replace() function only replaces the first match (see more here: JavaScript .replace...
View ArticleAnswer by Demian Brecht for Concatenating strings with `if` statements in...
I might do something a little different (a little more akin to templating), mainly because I hate concatenated HTML done with Javascript:var metadata_title = "Hello";var metadata_author = "Me";var...
View ArticleAnswer by Ray Toal for Concatenating strings with `if` statements in JavaScript
Build up the entire document into an array, then join with a "\n" at the end. (The rationale for this is of course to not have lots of new lines scattered all about! And if you are on IE7 or less,...
View ArticleAnswer by scessor for Concatenating strings with `if` statements in JavaScript
data = "<html>\n<head>\n"+ ( typeof metadata_title !== "undefined" ?"<title>"+ metadata_title +"</title>\n" :"" )+ ( typeof metadata_author !== "undefined" ?"<meta...
View ArticleAnswer by bluish for Concatenating strings with `if` statements in JavaScript
I'd use a ternary operator:data = "<html>\n"+"<head>\n"+ ( typeof metadata_title !== "undefined" ? "<title>"+ metadata_title +"</title>\n" : "" )+ ( typeof metadata_author !==...
View ArticleConcatenating strings with `if` statements in JavaScript
I'm attempting to set up a script to concatenate some variables inside a string if they exist, in order to place the appropriate metadata tags into a rendered HTML document.My concatenation code...
View Article