1

Hello stackoverflow community,

I'm trying to create a variable through an if statement and other variables but it doesn't work. If anyone could help me i would really appreciate it.

This is my code to create the returntouser variable

function userinput() {
  return returntouser = metaTitle + "\n" +  metaDescription + "\n" + metaKeywords;
}

Now i'm trying to make an if function out of the "metaTitle" variable. If an user don't write anyting in the textform, the "metaTitle" variable will not be added to the "returntouser" variable.

function userinput() {
  return returntouser = if (document.getElementById("userinputTitle").length > 0) { return metaTitle } + "\n" +  metaDescription + "\n" + metaKeywords;
}

Can someone tell me how i can add this if function to either add the "metaTitle" variable to the "returntouser" variable or not

Truzze
  • 128
  • 1
  • 7
  • An if statement is exactly that: a ***statement***. You can't put it in an expression. You can use a ternary expression, but for this it would probably be better to put the whole if statement before the last line. The assignment to a free variable `returntouser` seems odd and unnecessary. You're already returning the value from the function. It seems like you're trying way too hard to fit all your code on one line. – Paul Sep 26 '15 at 23:47
  • 2
    You could use the ternary operator? – Script47 Sep 26 '15 at 23:48
  • 1
    possible duplicate of [How to write an inline IF statement in JavaScript?](http://stackoverflow.com/questions/10270351/how-to-write-an-inline-if-statement-in-javascript) – Robbie Wxyz Sep 26 '15 at 23:52

3 Answers3

6

You can do a conditional inline using the ? - operator. It is also called the ternary operator. More information here.

Example:

value = 10 > 3 ? "10 is bigger than 3" : "10 is smaller than 3";
ergonaut
  • 6,929
  • 1
  • 17
  • 47
1

You have to do it in separate steps.

function userinput() {
  var returntouser = "";
  if (document.getElementById("userinputTitle").length > 0) { returntouser += metaTitle } 
  return returntouser + "\n" + metaDescription + "\n" + metaKeywords;
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

Try splitting up your actions:

function userinput() {
  var meta = metaDescription + "\n" + metaKeywords;
  if (document.getElementById("userinputTitle").length == 0) return meta; 
  return metaTitle  + "\n" + meta; 
}

or for learning purposes:

function userinput() {
  var meta = [metaDescription, metaKeywords];
  if (document.getElementById("userinputTitle")) meta.unshift(metaTitle);
  return meta.join('\n'); 
}

or in more modern browsers

function userinput() {
  var mt = document.getElementById('userinputTitle')? `${metaTitle}\n` : '';
  return `${mt}${metaDescription}\n${metaKeywords}`; 
}
rism
  • 11,932
  • 16
  • 76
  • 116
  • Thanks for the help :) – Truzze Sep 26 '15 at 23:58
  • I didn't notice the template string at first and thought you were implying that the ternary operator is only available in modern browsers lol. – Paul Sep 27 '15 at 01:25
  • Templates are kinda long in the tooth now too but only just standardized in ES6. ergonaut already covered turnary so i didn't bother. – rism Sep 27 '15 at 04:49