+ 1
Why does this code prints not only the argument passed to the function but also an undefined type.
14 ответов
+ 1
I'm sorry for misunderstanding but what I was saying is, when you use "console.log" it mean that you are printing something on console.
//Declaration of a function to print a variable name on console
function sayName (name) {
console.log(name)
}
The when you write
console.log(sayName('e'))
It means you are parsing a value "e" to a function so the statement starts
with a function call.
Then on console.log() there is no variable that's why it prints "undefined"
This means when you call
console.log(sayName ('e'))
console.log(sayName ('f'))
It will print
e
undefined
f
undefined
+ 4
Yaroslav Vernigora thanks a lot for the explanation.
+ 4
Ok. Thank you for being so nice. I'll refer to it on my web development journey.
+ 3
Hi! because console.log after execution cannot return anything back.
function sayName(name){
console.log(name)
}
sayName('e')
+ 2
The “undefined” value
The special value undefined also stands apart. It makes a type of its own, just like null.
The meaning of undefined is “value is not assigned”.
If a variable is declared, but not assigned, then its value is undefined:
let age;
alert(age);
// shows "undefined"
Technically, it is possible to explicitly assign undefined to a variable:
let age = 100;
// change the value to undefined
age = undefined;
alert(age); // "undefined"
…But we don’t recommend doing that. Normally, one uses null to assign an “empty” or “unknown” value to a variable, while undefined is reserved as a default initial value for unassigned things.
+ 2
I'm advise you this site:
https://javascript.info/
+ 2
you're welcome
+ 1
Jessica Cristal Can you help me here please.
+ 1
Yaroslav Vernigora thanks for checking out my code but i wanted to know why does my code prints not only 'e' but also 'undefined'
+ 1
I wrote to you about it above. review again carefully my message.
+ 1
Manoti (Challenge Accepted) first of all you are wrong for assuming what i wanted to do(because i was just playing around to learn) And you also didn't answer why my code( console.log(sayName('e')) prints undefined.
0
You declared a function with it's statement to print the name.
Also when calling the function you are also using a statement to print your function. You should just call the function
function sayName(name){
console.log(name)
}
sayName("e")
- 2
Mina Adel
- 2
Hello