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.
CodePudding user response:
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
CodePudding user response:
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)
CodePudding user response:
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!