24
loading...
This website collects cookies to deliver better user experience
We can write :
let Username = "Namish";
let Username2 = "Iqbal"
console.log(`Hello ${Username} Have a nice day. Thanks to reaching out. I'll get back to you soon.);
console.log(`Hello ${Username2} Have a nice day. Thanks to reaching out. I'll get back to you soon.);
Output
10Functions.js:231 Hello Namish Have a nice day. Thanks to reaching out. I'll get back to you soon.
10Functions.js:233 Hello Iqbal Have a nice day. Thanks to reaching out. I'll get back to you soon.
functions
were introduced.Lets see same code with a function.
function greeting ()
{
let name = "Shashma"
console.log(`Hello ${name} Have a nice day. Thanks to reaching out. I'll get back to you soon.`);
}
greeting;
Output:
Hello Shashma Have a nice day. Thanks to reaching out. I'll get back to you soon.
What is arguments ?
You may ask quite often. I find it quite confusing in my early coding days.function greeting (name)
{
console.log(`Hello ${name} Have a nice day. Thanks to reaching out. I'll get back to you soon.`)
}
greeting('Shashma');
Output :
Hello Shashma Have a nice day. Thanks to reaching out. I'll get back to you soon.
function greeting(name)
is a placeholder for name. It's like fill in the blanks where you have to just put the value.function greeting(name, regards)
{
console.log(`Hello ${name} Have a nice day. Thanks to reaching out. I'll get back to you soon. ${regards}`)
greeting('Shashma', 'regards');
Output :
Hello Shashma Have a nice day. Thanks to reaching out. I'll get back to you soon regards.
But what if we don't pass the value in the function?
function greeting (name, regards)
{
console.log(`Hello ${name} Have a nice day. Thanks to reaching out. I'll get back to you soon.`)
}
greeting('Shashma');
Output :
Hello Shashma Have a nice day. Thanks to reaching out. I'll get back to you soon undefined.
function greeting (name, regards='thanks')
{
console.log(`Hello ${name} Have a nice day. Thanks to reaching out. I'll get back to you soon ${regards}.`)
}
greeting('Shashma');
Output :
Hello Shashma Have a nice day. Thanks to reaching out. I'll get back to you soon thanks.
function greeting (name, regards='thanks')
{
console.log(`Hello ${name} Have a nice day. Thanks to reaching out. I'll get back to you soon ${regards}.`)
}
greeting('Shashma' , 'regards');
Output :
Hello Shashma Have a nice day. Thanks to reaching out. I'll get back to you soon regards.