Functions in JavaScript(and near damn every programming language) are pieces of code/statements of code that perform a specific task. These statements are grouped together into a function for the main purpose of being reusable. Functions in JavaScript can be written in two different ways. the more recent ES6 format which is newest and will be used for most of the examples in this article goes as thus
const playMusic = (songName, artist ) => {
console.log(`${songName} was sang by {artist}`);
{
descriptively, playMusic is then name the function, inside the parenthesis are three paraments passed into a function. These parameters are then subsequently used in the body of the function to perform a task or a series of tasks. The parameters are how we pass information into a function. Now in the function declaration above, the values are the parenthesis are just placeholders. They aren't real values but placeholders for values that will be passed in the function is called/invoked.
The placeholder values in function declaration are termed parameters, while the values passed in when the function is called/evoked, are termed arguments:
playMusic('Happy', 'Pharrell');
Functions aren't only to be used to output something to the console. that would make a mockery of their might. In reality, production code scarcely uses console.log
in functions save only for purposes of debugging code. To get values out of functions, we make use of the return
key word inside the body of a function.
Using ES5 format
function profile(name, age)
{
return `I am ${name}. I'm actually ${age} years old but I pretend I'm much older`;
}
Choice, in philosophy, is the supposed ability to freely decide between alternatives. Choice is a corollary of the traditional notion of free will, understood as the supposed power or capacity of humans to make decisions or perform actions independently of any prior event in or state of the universe.
In JavaScript, to create options(concrete depictions of choices or alternatives), we used what we call a conditional. A conditional lets a programming execution phase to decide between two or more probabilities all hinging on the value of an expression.
the simplest conditional are if...else
statements.
let GOAT = ' ';
if(GOAT === 'pac')
{
console.log(`Somebody help me, tell me where to go from here cause even thugs cry, but do the Lord care?`)
console.log(`We all gonna die, we bleed from similar veins`)
}
else if(GOAT === 'Biggie')
{
console.log(`Stay far from timid, only make moves when your heart’s in it, and live the phrase ‘sky’s the limit`)
console.log(`A strong word called consignment strictly for live men, not for freshmen, if you ain’t got the clientele say “hell no!” ‘Cause they gon’ want they money, rain, sleet, hail, snow.`)
}