-1

Do javaScript accept multiple strings as a function parameter?

function name(Haydon Lyson){
//code
} 

will it be accepted? If not then how can we pass multiple strings as function parameter.

3
  • 3
    Please see this Commented Jun 25, 2022 at 5:03
  • Not sure what the question is. Do you want to pass multiple string literals or multiple variables containing string literals? As per your question "Haydon" and "Lyson" seems like string literals and not variables (strange name for variables) Please clarify. Commented Jun 25, 2022 at 5:15
  • Yes, can we pass multiple string literals as parameters?? Commented Jun 26, 2022 at 8:59

3 Answers 3

2

You can use the rest parameter for that rest parament

function name(...params){
  // ...params return an array of parameters
  // like this [hadon ,dsjfl,sdfl];


}
       or


function name(name1,name2,name3,name4){
// you can use multiple params if you know params exact length
// if you don't length of params then 
// use above methods
Sign up to request clarification or add additional context in comments.

3 Comments

I think this might be a little more advanced than what the OP is looking for.
what is OP??? means
OP stands for Original Poster. Here that makes it "Ivanovich" who asked the question.
1

You can just pass them in separating them by comma:

function name(haydon, lyson) {
   console.log(haydon, layson)
}
let haydon = 1
let lyson = 5

name(hayden, lyson)

Comments

1

Passing multiple strings as a function parameter can be done one of two ways.

The first is by taking in the strings as parameters and separating them by commas.

var haydon = "Haydon";
var lyson = "Lyson";

function name(haydon, lyson){
//code
} 

The second way is to pass in an object into your function so that you can have the ability to manipulate the key-value pairs into strings in your function, allowing for a massive amount of inputs with just one single parameter.

let names = {
    cersei: 'Lannister',
    arya: 'Stark',
    jon: 'Snow',
    brienne: 'Tarth',
    daenerys: 'Targaryen',
    theon: 'Greyjoy',
    jorah: 'Mormont',
    margaery: 'Tyrell',
    sandor: 'Clegane',
    samwell: 'Tarly',
    ramsay: 'Bolton',
    haydon: 'Lyson'
}

function name(names){
//code
} 

Hope this helps!

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.