Function without parentheses returns a weird output
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
Just asking to understand how it works:
function say(something)
return something;
let name = `Reza`;
console.log(say `My name is`, name, '!');
It returns a very weird output. I think that My name is
is a string within an array and everything else is just a string (correct me if I am wrong).
My Question is, what is the point of that and when would it make sense to use a function like that?
Also I would be glad if someone could tell me why My name is $name
is not working (name
returns as an empty string).
PS: I know that could use the function with parentheses and it would work but that's not my question.
javascript
add a comment |Â
up vote
6
down vote
favorite
Just asking to understand how it works:
function say(something)
return something;
let name = `Reza`;
console.log(say `My name is`, name, '!');
It returns a very weird output. I think that My name is
is a string within an array and everything else is just a string (correct me if I am wrong).
My Question is, what is the point of that and when would it make sense to use a function like that?
Also I would be glad if someone could tell me why My name is $name
is not working (name
returns as an empty string).
PS: I know that could use the function with parentheses and it would work but that's not my question.
javascript
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Just asking to understand how it works:
function say(something)
return something;
let name = `Reza`;
console.log(say `My name is`, name, '!');
It returns a very weird output. I think that My name is
is a string within an array and everything else is just a string (correct me if I am wrong).
My Question is, what is the point of that and when would it make sense to use a function like that?
Also I would be glad if someone could tell me why My name is $name
is not working (name
returns as an empty string).
PS: I know that could use the function with parentheses and it would work but that's not my question.
javascript
Just asking to understand how it works:
function say(something)
return something;
let name = `Reza`;
console.log(say `My name is`, name, '!');
It returns a very weird output. I think that My name is
is a string within an array and everything else is just a string (correct me if I am wrong).
My Question is, what is the point of that and when would it make sense to use a function like that?
Also I would be glad if someone could tell me why My name is $name
is not working (name
returns as an empty string).
PS: I know that could use the function with parentheses and it would work but that's not my question.
function say(something)
return something;
let name = `Reza`;
console.log(say `My name is`, name, '!');
function say(something)
return something;
let name = `Reza`;
console.log(say `My name is`, name, '!');
javascript
javascript
asked 6 hours ago


Reza Saadati
383113
383113
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
3
down vote
why My name is $name is not working (name returns as an empty
string).
This is because you have to pull out the value of name
from the array of string values and return it
Try this:
function say(something)
var str0 = something[0]
return str0;
let name = `Reza`;
console.log(say`My name is$name`, name, '!'); // My name is Reza !
We will receive thing like this:
This is the result after running your codes on the Chrome devtools:
Let's see how we can explain what is actually going on. Well then, what you're using is called Tagged templates
:
A more advanced form of template literals are tagged templates. Tags allow you to parse template literals with a function. The first argument of a tag function contains an array of string values.
That's why we see an array
in the result:
For instance, if we run this codes on Chrome devtools:
function say(something)
return something;
console.log(say`anything`);
On the console tab, we will receive this result:
For more information, you could read it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates
As we can see, the weird raw
property, according to MDN - Raw strings, it is:
The special raw property, available on the first function argument of tagged templates, allows you to access the raw strings as they were entered, without processing escape sequences.
For example:
function tag(strings)
console.log(strings.raw[0]);
tag`string text line 1 n string text line 2`;
// logs "string text line 1 n string text line 2" ,
// including the two characters '' and 'n'
And you are using the console.log() method, according to MDN-Console.log() we have its signature:
Syntax: Console.log()
console.log(obj1 [, obj2, ..., objN]);
console.log(msg [, subst1, ..., substN]);
Parameters
'obj1 ... objN'
A list of JavaScript objects to output. The string representations of
each of these objects are appended together in the order listed and
output.
msg
A JavaScript string containing zero or more substitution strings.
subst1 ... substN
JavaScript objects with which to replace substitution strings within
msg. This gives you additional control over the format of the output.
In your case, you're passing multiple arguments into the Console.log() method, hence it will work as the document said here: Outputting multiple objects
add a comment |Â
up vote
1
down vote
say
tag is missing the arguments to the placeholders in the template string:
function shout(parts, name/*<--HERE*/)
return `$parts[0]$name$parts[1]!!!`;
let name = `Reza`;
console.log(shout `My name is $name`);
say
receives data from the template string as a split string around the placeholders:
[
"My name is ",
"!"
] Reza
Look at this example from the MDN Documentation:
function myTag(strings, personExp, ageExp)
var str0 = strings[0]; // "That "
var str1 = strings[1]; // " is a "
var ageStr;
if (ageExp > 99)
ageStr = 'centenarian';
else
ageStr = 'youngster';
// We can even return a string built using a template literal
return `$str0$personExp$str1$ageStr`;
var person = 'Mike';
var age = 28;
var output = myTag`That $ person is a $ age `;
add a comment |Â
up vote
0
down vote
Doing My name is $name
should work. Its because your passing My name is
to the say function as a tagged template literal, which by default passes the arguments as an array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
add a comment |Â
up vote
0
down vote
Your tagged template has a missing argument, it should include a strings array argument as first argument.
function say(strings, something)
return strings[0] + "Mr. " + something +"!";
let name = `Reza`;
console.log(say `My name is $name`);
add a comment |Â
up vote
0
down vote
Make it simple
console.log `Hi there`;
why My name is $name is not working (name returns as an empty string).
console.log(say `My name is`, name, '!'); /* I don't see `$name` here */
Also, name
was never passed as an argument to say
function!
function say(something)
return something;
let name = `Reza`;
/* say without () */
console.log(say `My name is $name !`);
/* say with () */
console.log(say(`My name is $name !`));
let who = 'everyone';
console.log `Hello $who`; /* note, no -> () */
console.log(`Hello $who`);
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
why My name is $name is not working (name returns as an empty
string).
This is because you have to pull out the value of name
from the array of string values and return it
Try this:
function say(something)
var str0 = something[0]
return str0;
let name = `Reza`;
console.log(say`My name is$name`, name, '!'); // My name is Reza !
We will receive thing like this:
This is the result after running your codes on the Chrome devtools:
Let's see how we can explain what is actually going on. Well then, what you're using is called Tagged templates
:
A more advanced form of template literals are tagged templates. Tags allow you to parse template literals with a function. The first argument of a tag function contains an array of string values.
That's why we see an array
in the result:
For instance, if we run this codes on Chrome devtools:
function say(something)
return something;
console.log(say`anything`);
On the console tab, we will receive this result:
For more information, you could read it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates
As we can see, the weird raw
property, according to MDN - Raw strings, it is:
The special raw property, available on the first function argument of tagged templates, allows you to access the raw strings as they were entered, without processing escape sequences.
For example:
function tag(strings)
console.log(strings.raw[0]);
tag`string text line 1 n string text line 2`;
// logs "string text line 1 n string text line 2" ,
// including the two characters '' and 'n'
And you are using the console.log() method, according to MDN-Console.log() we have its signature:
Syntax: Console.log()
console.log(obj1 [, obj2, ..., objN]);
console.log(msg [, subst1, ..., substN]);
Parameters
'obj1 ... objN'
A list of JavaScript objects to output. The string representations of
each of these objects are appended together in the order listed and
output.
msg
A JavaScript string containing zero or more substitution strings.
subst1 ... substN
JavaScript objects with which to replace substitution strings within
msg. This gives you additional control over the format of the output.
In your case, you're passing multiple arguments into the Console.log() method, hence it will work as the document said here: Outputting multiple objects
add a comment |Â
up vote
3
down vote
why My name is $name is not working (name returns as an empty
string).
This is because you have to pull out the value of name
from the array of string values and return it
Try this:
function say(something)
var str0 = something[0]
return str0;
let name = `Reza`;
console.log(say`My name is$name`, name, '!'); // My name is Reza !
We will receive thing like this:
This is the result after running your codes on the Chrome devtools:
Let's see how we can explain what is actually going on. Well then, what you're using is called Tagged templates
:
A more advanced form of template literals are tagged templates. Tags allow you to parse template literals with a function. The first argument of a tag function contains an array of string values.
That's why we see an array
in the result:
For instance, if we run this codes on Chrome devtools:
function say(something)
return something;
console.log(say`anything`);
On the console tab, we will receive this result:
For more information, you could read it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates
As we can see, the weird raw
property, according to MDN - Raw strings, it is:
The special raw property, available on the first function argument of tagged templates, allows you to access the raw strings as they were entered, without processing escape sequences.
For example:
function tag(strings)
console.log(strings.raw[0]);
tag`string text line 1 n string text line 2`;
// logs "string text line 1 n string text line 2" ,
// including the two characters '' and 'n'
And you are using the console.log() method, according to MDN-Console.log() we have its signature:
Syntax: Console.log()
console.log(obj1 [, obj2, ..., objN]);
console.log(msg [, subst1, ..., substN]);
Parameters
'obj1 ... objN'
A list of JavaScript objects to output. The string representations of
each of these objects are appended together in the order listed and
output.
msg
A JavaScript string containing zero or more substitution strings.
subst1 ... substN
JavaScript objects with which to replace substitution strings within
msg. This gives you additional control over the format of the output.
In your case, you're passing multiple arguments into the Console.log() method, hence it will work as the document said here: Outputting multiple objects
add a comment |Â
up vote
3
down vote
up vote
3
down vote
why My name is $name is not working (name returns as an empty
string).
This is because you have to pull out the value of name
from the array of string values and return it
Try this:
function say(something)
var str0 = something[0]
return str0;
let name = `Reza`;
console.log(say`My name is$name`, name, '!'); // My name is Reza !
We will receive thing like this:
This is the result after running your codes on the Chrome devtools:
Let's see how we can explain what is actually going on. Well then, what you're using is called Tagged templates
:
A more advanced form of template literals are tagged templates. Tags allow you to parse template literals with a function. The first argument of a tag function contains an array of string values.
That's why we see an array
in the result:
For instance, if we run this codes on Chrome devtools:
function say(something)
return something;
console.log(say`anything`);
On the console tab, we will receive this result:
For more information, you could read it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates
As we can see, the weird raw
property, according to MDN - Raw strings, it is:
The special raw property, available on the first function argument of tagged templates, allows you to access the raw strings as they were entered, without processing escape sequences.
For example:
function tag(strings)
console.log(strings.raw[0]);
tag`string text line 1 n string text line 2`;
// logs "string text line 1 n string text line 2" ,
// including the two characters '' and 'n'
And you are using the console.log() method, according to MDN-Console.log() we have its signature:
Syntax: Console.log()
console.log(obj1 [, obj2, ..., objN]);
console.log(msg [, subst1, ..., substN]);
Parameters
'obj1 ... objN'
A list of JavaScript objects to output. The string representations of
each of these objects are appended together in the order listed and
output.
msg
A JavaScript string containing zero or more substitution strings.
subst1 ... substN
JavaScript objects with which to replace substitution strings within
msg. This gives you additional control over the format of the output.
In your case, you're passing multiple arguments into the Console.log() method, hence it will work as the document said here: Outputting multiple objects
why My name is $name is not working (name returns as an empty
string).
This is because you have to pull out the value of name
from the array of string values and return it
Try this:
function say(something)
var str0 = something[0]
return str0;
let name = `Reza`;
console.log(say`My name is$name`, name, '!'); // My name is Reza !
We will receive thing like this:
This is the result after running your codes on the Chrome devtools:
Let's see how we can explain what is actually going on. Well then, what you're using is called Tagged templates
:
A more advanced form of template literals are tagged templates. Tags allow you to parse template literals with a function. The first argument of a tag function contains an array of string values.
That's why we see an array
in the result:
For instance, if we run this codes on Chrome devtools:
function say(something)
return something;
console.log(say`anything`);
On the console tab, we will receive this result:
For more information, you could read it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates
As we can see, the weird raw
property, according to MDN - Raw strings, it is:
The special raw property, available on the first function argument of tagged templates, allows you to access the raw strings as they were entered, without processing escape sequences.
For example:
function tag(strings)
console.log(strings.raw[0]);
tag`string text line 1 n string text line 2`;
// logs "string text line 1 n string text line 2" ,
// including the two characters '' and 'n'
And you are using the console.log() method, according to MDN-Console.log() we have its signature:
Syntax: Console.log()
console.log(obj1 [, obj2, ..., objN]);
console.log(msg [, subst1, ..., substN]);
Parameters
'obj1 ... objN'
A list of JavaScript objects to output. The string representations of
each of these objects are appended together in the order listed and
output.
msg
A JavaScript string containing zero or more substitution strings.
subst1 ... substN
JavaScript objects with which to replace substitution strings within
msg. This gives you additional control over the format of the output.
In your case, you're passing multiple arguments into the Console.log() method, hence it will work as the document said here: Outputting multiple objects
function say(something)
var str0 = something[0]
return str0;
let name = `Reza`;
console.log(say`My name is$name`, name, '!'); // My name is Reza !
function say(something)
var str0 = something[0]
return str0;
let name = `Reza`;
console.log(say`My name is$name`, name, '!'); // My name is Reza !
edited 4 hours ago
answered 6 hours ago
Nguyễn Thanh Tú
1,258114
1,258114
add a comment |Â
add a comment |Â
up vote
1
down vote
say
tag is missing the arguments to the placeholders in the template string:
function shout(parts, name/*<--HERE*/)
return `$parts[0]$name$parts[1]!!!`;
let name = `Reza`;
console.log(shout `My name is $name`);
say
receives data from the template string as a split string around the placeholders:
[
"My name is ",
"!"
] Reza
Look at this example from the MDN Documentation:
function myTag(strings, personExp, ageExp)
var str0 = strings[0]; // "That "
var str1 = strings[1]; // " is a "
var ageStr;
if (ageExp > 99)
ageStr = 'centenarian';
else
ageStr = 'youngster';
// We can even return a string built using a template literal
return `$str0$personExp$str1$ageStr`;
var person = 'Mike';
var age = 28;
var output = myTag`That $ person is a $ age `;
add a comment |Â
up vote
1
down vote
say
tag is missing the arguments to the placeholders in the template string:
function shout(parts, name/*<--HERE*/)
return `$parts[0]$name$parts[1]!!!`;
let name = `Reza`;
console.log(shout `My name is $name`);
say
receives data from the template string as a split string around the placeholders:
[
"My name is ",
"!"
] Reza
Look at this example from the MDN Documentation:
function myTag(strings, personExp, ageExp)
var str0 = strings[0]; // "That "
var str1 = strings[1]; // " is a "
var ageStr;
if (ageExp > 99)
ageStr = 'centenarian';
else
ageStr = 'youngster';
// We can even return a string built using a template literal
return `$str0$personExp$str1$ageStr`;
var person = 'Mike';
var age = 28;
var output = myTag`That $ person is a $ age `;
add a comment |Â
up vote
1
down vote
up vote
1
down vote
say
tag is missing the arguments to the placeholders in the template string:
function shout(parts, name/*<--HERE*/)
return `$parts[0]$name$parts[1]!!!`;
let name = `Reza`;
console.log(shout `My name is $name`);
say
receives data from the template string as a split string around the placeholders:
[
"My name is ",
"!"
] Reza
Look at this example from the MDN Documentation:
function myTag(strings, personExp, ageExp)
var str0 = strings[0]; // "That "
var str1 = strings[1]; // " is a "
var ageStr;
if (ageExp > 99)
ageStr = 'centenarian';
else
ageStr = 'youngster';
// We can even return a string built using a template literal
return `$str0$personExp$str1$ageStr`;
var person = 'Mike';
var age = 28;
var output = myTag`That $ person is a $ age `;
say
tag is missing the arguments to the placeholders in the template string:
function shout(parts, name/*<--HERE*/)
return `$parts[0]$name$parts[1]!!!`;
let name = `Reza`;
console.log(shout `My name is $name`);
say
receives data from the template string as a split string around the placeholders:
[
"My name is ",
"!"
] Reza
Look at this example from the MDN Documentation:
function myTag(strings, personExp, ageExp)
var str0 = strings[0]; // "That "
var str1 = strings[1]; // " is a "
var ageStr;
if (ageExp > 99)
ageStr = 'centenarian';
else
ageStr = 'youngster';
// We can even return a string built using a template literal
return `$str0$personExp$str1$ageStr`;
var person = 'Mike';
var age = 28;
var output = myTag`That $ person is a $ age `;
function shout(parts, name/*<--HERE*/)
return `$parts[0]$name$parts[1]!!!`;
let name = `Reza`;
console.log(shout `My name is $name`);
function shout(parts, name/*<--HERE*/)
return `$parts[0]$name$parts[1]!!!`;
let name = `Reza`;
console.log(shout `My name is $name`);
edited 6 hours ago
answered 6 hours ago


Rafael
3,30751432
3,30751432
add a comment |Â
add a comment |Â
up vote
0
down vote
Doing My name is $name
should work. Its because your passing My name is
to the say function as a tagged template literal, which by default passes the arguments as an array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
add a comment |Â
up vote
0
down vote
Doing My name is $name
should work. Its because your passing My name is
to the say function as a tagged template literal, which by default passes the arguments as an array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Doing My name is $name
should work. Its because your passing My name is
to the say function as a tagged template literal, which by default passes the arguments as an array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
Doing My name is $name
should work. Its because your passing My name is
to the say function as a tagged template literal, which by default passes the arguments as an array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
answered 6 hours ago
jman93
616
616
add a comment |Â
add a comment |Â
up vote
0
down vote
Your tagged template has a missing argument, it should include a strings array argument as first argument.
function say(strings, something)
return strings[0] + "Mr. " + something +"!";
let name = `Reza`;
console.log(say `My name is $name`);
add a comment |Â
up vote
0
down vote
Your tagged template has a missing argument, it should include a strings array argument as first argument.
function say(strings, something)
return strings[0] + "Mr. " + something +"!";
let name = `Reza`;
console.log(say `My name is $name`);
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Your tagged template has a missing argument, it should include a strings array argument as first argument.
function say(strings, something)
return strings[0] + "Mr. " + something +"!";
let name = `Reza`;
console.log(say `My name is $name`);
Your tagged template has a missing argument, it should include a strings array argument as first argument.
function say(strings, something)
return strings[0] + "Mr. " + something +"!";
let name = `Reza`;
console.log(say `My name is $name`);
answered 5 hours ago
Calvin
593
593
add a comment |Â
add a comment |Â
up vote
0
down vote
Make it simple
console.log `Hi there`;
why My name is $name is not working (name returns as an empty string).
console.log(say `My name is`, name, '!'); /* I don't see `$name` here */
Also, name
was never passed as an argument to say
function!
function say(something)
return something;
let name = `Reza`;
/* say without () */
console.log(say `My name is $name !`);
/* say with () */
console.log(say(`My name is $name !`));
let who = 'everyone';
console.log `Hello $who`; /* note, no -> () */
console.log(`Hello $who`);
add a comment |Â
up vote
0
down vote
Make it simple
console.log `Hi there`;
why My name is $name is not working (name returns as an empty string).
console.log(say `My name is`, name, '!'); /* I don't see `$name` here */
Also, name
was never passed as an argument to say
function!
function say(something)
return something;
let name = `Reza`;
/* say without () */
console.log(say `My name is $name !`);
/* say with () */
console.log(say(`My name is $name !`));
let who = 'everyone';
console.log `Hello $who`; /* note, no -> () */
console.log(`Hello $who`);
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Make it simple
console.log `Hi there`;
why My name is $name is not working (name returns as an empty string).
console.log(say `My name is`, name, '!'); /* I don't see `$name` here */
Also, name
was never passed as an argument to say
function!
function say(something)
return something;
let name = `Reza`;
/* say without () */
console.log(say `My name is $name !`);
/* say with () */
console.log(say(`My name is $name !`));
let who = 'everyone';
console.log `Hello $who`; /* note, no -> () */
console.log(`Hello $who`);
Make it simple
console.log `Hi there`;
why My name is $name is not working (name returns as an empty string).
console.log(say `My name is`, name, '!'); /* I don't see `$name` here */
Also, name
was never passed as an argument to say
function!
function say(something)
return something;
let name = `Reza`;
/* say without () */
console.log(say `My name is $name !`);
/* say with () */
console.log(say(`My name is $name !`));
let who = 'everyone';
console.log `Hello $who`; /* note, no -> () */
console.log(`Hello $who`);
console.log `Hi there`;
console.log `Hi there`;
function say(something)
return something;
let name = `Reza`;
/* say without () */
console.log(say `My name is $name !`);
/* say with () */
console.log(say(`My name is $name !`));
function say(something)
return something;
let name = `Reza`;
/* say without () */
console.log(say `My name is $name !`);
/* say with () */
console.log(say(`My name is $name !`));
let who = 'everyone';
console.log `Hello $who`; /* note, no -> () */
console.log(`Hello $who`);
let who = 'everyone';
console.log `Hello $who`; /* note, no -> () */
console.log(`Hello $who`);
edited 5 hours ago
answered 5 hours ago


Arvind
15.5k11531
15.5k11531
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52573941%2ffunction-without-parentheses-returns-a-weird-output%23new-answer', 'question_page');
);
Post as a guest
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
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
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