0

This calls me to concatenate two strings that are not yet declared they are still in parameter

I have tried to give the parameter values in string form and have console log the strings to become arguments in console.log(string + string2)

function concatenateTwoStrings(string1, string2) {

  var string1 = "dance"
  var string2 = "party"
}
console.log(string1 + string2)

expected result is danceparty actual result is syntax error.

2
  • 1
    Did you try console.log(string1+string2) inside the function as well? Commented Aug 12, 2019 at 15:48
  • 2
    You should consider naming your variables and parameters differently. Commented Aug 12, 2019 at 15:50

2 Answers 2

3

JavaScript has function scoping which means that string1 and string2 are only accessible from within the concatenateTwoStrings function.

Where you have the console.log statement, the variables are out of scope.

Additionally, you probably want to pass the strings to the function instead of defining them inside of the function.

Maybe what you want to do is put the console.log inside of the function and call the function with "dance" and "party" as parameters:

function concatenateTwoStrings(string1, string2) {
  console.log(string1 + string2)
}

concatenateTwoStrings("dance", "party")

Or you may want to return the concatenated string and then console.log result:

function concatenateTwoStrings(string1, string2) {
  return string1 + string2;
}

console.log(concatenateTwoStrings("dance", "party"))


Further Resources:

Sign up to request clarification or add additional context in comments.

Comments

1

You have to define your variables outside of the function if you want them passed as a parameters. You also have to pass those arguments into the function and invoke the function.

var string1 = "dance"
var string2 = "party"

function concatenateTwoStrings(string1, string2) {
  return string1 + string2;
}
console.log(concatenateTwoStrings(string1, string2))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.