JavaScript let if else statement args
I'm trying to define a let in JavaScript so that if there's no arg[2]
, then it will just use arg[1]
, but if there is arg[1]
and arg[2]
it will use them both.
ie. command: !hello world
then: let cmd = arg[1]
but also
command: !hello world one
then: let cmd = arg[1] + " " + arg[2]
I have this, however it only works if arg[2]
exists, and doesn't if only arg[1]
is supplied:
let cmd = args[1] + " " + args[2];
Is there anyway you can specify an arg to pass through spaces? So !hello world one, would be arg[1] arg[2] (arg[2]
being: world one)
javascript let
add a comment |
I'm trying to define a let in JavaScript so that if there's no arg[2]
, then it will just use arg[1]
, but if there is arg[1]
and arg[2]
it will use them both.
ie. command: !hello world
then: let cmd = arg[1]
but also
command: !hello world one
then: let cmd = arg[1] + " " + arg[2]
I have this, however it only works if arg[2]
exists, and doesn't if only arg[1]
is supplied:
let cmd = args[1] + " " + args[2];
Is there anyway you can specify an arg to pass through spaces? So !hello world one, would be arg[1] arg[2] (arg[2]
being: world one)
javascript let
1
You seem to be asking two very different questions: (1) how to deal with an optional second argument and (2) how to pass an argument that contains spaces. It's not clear if you're asking the second question as a way of dealing with the first or if it's a separate issue. The first issue can be handled with a ternary expression:let cmd = arg[2] === undefined ? arg[1] + " " + arg[2] : arg[1];
.
– Ted Hopp
Nov 14 '18 at 1:58
1
let cmd = args.join(" ");
would suffice if you didn't mind them possibly supplying more than two arguments.
– Tyler Roper
Nov 14 '18 at 2:02
Thank you. @TedHopp, how would I go about the second, where args[3] would contain everything from args[3] and onward?
– ah1
Nov 14 '18 at 18:58
1
You can use what @Tyler suggested and collect all remaining arguments with something likeargs.slice(3).join(" ")
(which would create a single string containing all elements ofarg
from index 3 onward, separated by a space).
– Ted Hopp
Nov 14 '18 at 19:36
add a comment |
I'm trying to define a let in JavaScript so that if there's no arg[2]
, then it will just use arg[1]
, but if there is arg[1]
and arg[2]
it will use them both.
ie. command: !hello world
then: let cmd = arg[1]
but also
command: !hello world one
then: let cmd = arg[1] + " " + arg[2]
I have this, however it only works if arg[2]
exists, and doesn't if only arg[1]
is supplied:
let cmd = args[1] + " " + args[2];
Is there anyway you can specify an arg to pass through spaces? So !hello world one, would be arg[1] arg[2] (arg[2]
being: world one)
javascript let
I'm trying to define a let in JavaScript so that if there's no arg[2]
, then it will just use arg[1]
, but if there is arg[1]
and arg[2]
it will use them both.
ie. command: !hello world
then: let cmd = arg[1]
but also
command: !hello world one
then: let cmd = arg[1] + " " + arg[2]
I have this, however it only works if arg[2]
exists, and doesn't if only arg[1]
is supplied:
let cmd = args[1] + " " + args[2];
Is there anyway you can specify an arg to pass through spaces? So !hello world one, would be arg[1] arg[2] (arg[2]
being: world one)
javascript let
javascript let
edited Nov 14 '18 at 1:58
Pedro Lobito
48.4k14133164
48.4k14133164
asked Nov 14 '18 at 1:51
ah1ah1
168
168
1
You seem to be asking two very different questions: (1) how to deal with an optional second argument and (2) how to pass an argument that contains spaces. It's not clear if you're asking the second question as a way of dealing with the first or if it's a separate issue. The first issue can be handled with a ternary expression:let cmd = arg[2] === undefined ? arg[1] + " " + arg[2] : arg[1];
.
– Ted Hopp
Nov 14 '18 at 1:58
1
let cmd = args.join(" ");
would suffice if you didn't mind them possibly supplying more than two arguments.
– Tyler Roper
Nov 14 '18 at 2:02
Thank you. @TedHopp, how would I go about the second, where args[3] would contain everything from args[3] and onward?
– ah1
Nov 14 '18 at 18:58
1
You can use what @Tyler suggested and collect all remaining arguments with something likeargs.slice(3).join(" ")
(which would create a single string containing all elements ofarg
from index 3 onward, separated by a space).
– Ted Hopp
Nov 14 '18 at 19:36
add a comment |
1
You seem to be asking two very different questions: (1) how to deal with an optional second argument and (2) how to pass an argument that contains spaces. It's not clear if you're asking the second question as a way of dealing with the first or if it's a separate issue. The first issue can be handled with a ternary expression:let cmd = arg[2] === undefined ? arg[1] + " " + arg[2] : arg[1];
.
– Ted Hopp
Nov 14 '18 at 1:58
1
let cmd = args.join(" ");
would suffice if you didn't mind them possibly supplying more than two arguments.
– Tyler Roper
Nov 14 '18 at 2:02
Thank you. @TedHopp, how would I go about the second, where args[3] would contain everything from args[3] and onward?
– ah1
Nov 14 '18 at 18:58
1
You can use what @Tyler suggested and collect all remaining arguments with something likeargs.slice(3).join(" ")
(which would create a single string containing all elements ofarg
from index 3 onward, separated by a space).
– Ted Hopp
Nov 14 '18 at 19:36
1
1
You seem to be asking two very different questions: (1) how to deal with an optional second argument and (2) how to pass an argument that contains spaces. It's not clear if you're asking the second question as a way of dealing with the first or if it's a separate issue. The first issue can be handled with a ternary expression:
let cmd = arg[2] === undefined ? arg[1] + " " + arg[2] : arg[1];
.– Ted Hopp
Nov 14 '18 at 1:58
You seem to be asking two very different questions: (1) how to deal with an optional second argument and (2) how to pass an argument that contains spaces. It's not clear if you're asking the second question as a way of dealing with the first or if it's a separate issue. The first issue can be handled with a ternary expression:
let cmd = arg[2] === undefined ? arg[1] + " " + arg[2] : arg[1];
.– Ted Hopp
Nov 14 '18 at 1:58
1
1
let cmd = args.join(" ");
would suffice if you didn't mind them possibly supplying more than two arguments.– Tyler Roper
Nov 14 '18 at 2:02
let cmd = args.join(" ");
would suffice if you didn't mind them possibly supplying more than two arguments.– Tyler Roper
Nov 14 '18 at 2:02
Thank you. @TedHopp, how would I go about the second, where args[3] would contain everything from args[3] and onward?
– ah1
Nov 14 '18 at 18:58
Thank you. @TedHopp, how would I go about the second, where args[3] would contain everything from args[3] and onward?
– ah1
Nov 14 '18 at 18:58
1
1
You can use what @Tyler suggested and collect all remaining arguments with something like
args.slice(3).join(" ")
(which would create a single string containing all elements of arg
from index 3 onward, separated by a space).– Ted Hopp
Nov 14 '18 at 19:36
You can use what @Tyler suggested and collect all remaining arguments with something like
args.slice(3).join(" ")
(which would create a single string containing all elements of arg
from index 3 onward, separated by a space).– Ted Hopp
Nov 14 '18 at 19:36
add a comment |
3 Answers
3
active
oldest
votes
I usually do something like:
const myFunction = (argA, argB) => {
if (argB === undefined) argB = "";
console.log(argA + " " + argB);
return "" + argA + " " + argB
}
add a comment |
You mean like:
let test = arg[1] && arg[2] ? arg[1]+' '+arg[1] : arg[1];
add a comment |
If you are using ES6 you can use the rest operator like so:
const myFunction = (...args) => {
console.log(args[0])
}
if you don't use the fat arrow syntax you can refer to the arguments key word:
function func1(a, b, c) {
if(arguments && arguments.length == 2){
console.log('do a thing')
}else{
console.log('do a different thing')
}
};
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292069%2fjavascript-let-if-else-statement-args%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I usually do something like:
const myFunction = (argA, argB) => {
if (argB === undefined) argB = "";
console.log(argA + " " + argB);
return "" + argA + " " + argB
}
add a comment |
I usually do something like:
const myFunction = (argA, argB) => {
if (argB === undefined) argB = "";
console.log(argA + " " + argB);
return "" + argA + " " + argB
}
add a comment |
I usually do something like:
const myFunction = (argA, argB) => {
if (argB === undefined) argB = "";
console.log(argA + " " + argB);
return "" + argA + " " + argB
}
I usually do something like:
const myFunction = (argA, argB) => {
if (argB === undefined) argB = "";
console.log(argA + " " + argB);
return "" + argA + " " + argB
}
answered Nov 14 '18 at 1:55
FallenreaperFallenreaper
3,95883483
3,95883483
add a comment |
add a comment |
You mean like:
let test = arg[1] && arg[2] ? arg[1]+' '+arg[1] : arg[1];
add a comment |
You mean like:
let test = arg[1] && arg[2] ? arg[1]+' '+arg[1] : arg[1];
add a comment |
You mean like:
let test = arg[1] && arg[2] ? arg[1]+' '+arg[1] : arg[1];
You mean like:
let test = arg[1] && arg[2] ? arg[1]+' '+arg[1] : arg[1];
answered Nov 14 '18 at 2:01
Yoannes GeisslerYoannes Geissler
81118
81118
add a comment |
add a comment |
If you are using ES6 you can use the rest operator like so:
const myFunction = (...args) => {
console.log(args[0])
}
if you don't use the fat arrow syntax you can refer to the arguments key word:
function func1(a, b, c) {
if(arguments && arguments.length == 2){
console.log('do a thing')
}else{
console.log('do a different thing')
}
};
add a comment |
If you are using ES6 you can use the rest operator like so:
const myFunction = (...args) => {
console.log(args[0])
}
if you don't use the fat arrow syntax you can refer to the arguments key word:
function func1(a, b, c) {
if(arguments && arguments.length == 2){
console.log('do a thing')
}else{
console.log('do a different thing')
}
};
add a comment |
If you are using ES6 you can use the rest operator like so:
const myFunction = (...args) => {
console.log(args[0])
}
if you don't use the fat arrow syntax you can refer to the arguments key word:
function func1(a, b, c) {
if(arguments && arguments.length == 2){
console.log('do a thing')
}else{
console.log('do a different thing')
}
};
If you are using ES6 you can use the rest operator like so:
const myFunction = (...args) => {
console.log(args[0])
}
if you don't use the fat arrow syntax you can refer to the arguments key word:
function func1(a, b, c) {
if(arguments && arguments.length == 2){
console.log('do a thing')
}else{
console.log('do a different thing')
}
};
edited Nov 14 '18 at 2:11
answered Nov 14 '18 at 2:06
ruby_newbieruby_newbie
2,30521120
2,30521120
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292069%2fjavascript-let-if-else-statement-args%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You seem to be asking two very different questions: (1) how to deal with an optional second argument and (2) how to pass an argument that contains spaces. It's not clear if you're asking the second question as a way of dealing with the first or if it's a separate issue. The first issue can be handled with a ternary expression:
let cmd = arg[2] === undefined ? arg[1] + " " + arg[2] : arg[1];
.– Ted Hopp
Nov 14 '18 at 1:58
1
let cmd = args.join(" ");
would suffice if you didn't mind them possibly supplying more than two arguments.– Tyler Roper
Nov 14 '18 at 2:02
Thank you. @TedHopp, how would I go about the second, where args[3] would contain everything from args[3] and onward?
– ah1
Nov 14 '18 at 18:58
1
You can use what @Tyler suggested and collect all remaining arguments with something like
args.slice(3).join(" ")
(which would create a single string containing all elements ofarg
from index 3 onward, separated by a space).– Ted Hopp
Nov 14 '18 at 19:36