Writing functions in JavaScript


#1

Would the following code be considered a function in JavaScript that takes in a name input and logs a greeting to the console? Here is the code I wrote for the: var helloPerson = “Steven”
console.log(“Hello,”+ helloPerson);


#2

console describes the console object of the JavaScript namespace. .log() is a function, but more specifically it is a method of the console object. It takes an expression and outputs it to the display.

Regular users are not normally going to see this output since they will be viewing the HTML page and script behaviors associated with their user interaction.


#3

but you fail to answer the question as it related to the code


#4

Seems like it’s a yes/no question and that you should look up what a function is
Or that you’re really wondering something else

Difficult to answer. I skipped over it.


#5

in a sense I’m looking for a yes or no whether my code looks right but the most important thing is I’m ensuring that I understand how to write a function because i see some function are written with variables vs using the function keyword then log to the console. Thats what I’m trying to make sense of


#6

Then I’d say: no
And immediately agree that this isn’t a meaningful answer, but looking up what a function is both makes it meaningful (for what little it means) and lets you determine the same thing.


#7

to which I replied it is a function.


#8

ok. so to sum up that I’m understanding you correctly i don’t have to use the keyword function in order to write a function in JavaScript?


#9

What mtf means is that you use a function.
What you wrote is not a function (knowing what a function is makes this very obvious, really, go have a look at wikipedia and/or mdn)


#10

i’m looking on mdn still trying to brush up on it a bit i kind of understand trying to just be 100% certain


#11

Writing a function involves a declaration or an expression.

function funcName() {

}

var func = function() {

};

Using the function involves a call to invoke the function by the name given.

funcName()

func()

As you move forward another form will also emerge… Arrow functions, but they are a special type that requires background learning associated with the those illustrated above.


#12

Oh and regarding keywords, that really only means they’re reserved text that you can’t use for anything else. They don’t do anything whatsoever, they’re just part of syntax. So you might instead consider what syntax exists specifically to create functions and compare to your code to determine if it’s a match. That’s still horribly backwards though, to write something and then figure out if it means something (which is a big part of why your question is largely unanswerable, it’d involve taking several steps back, completely rephrasing your question, teaching you topics you should already know before asking the question, except you wouldn’t be asking it then)

I have sympathy for things being confusing and it being difficult to ask the right thing and all that. Doesn’t change the above though. The way around this is to reason only in terms you know, and carefully learn new things to build on.

Oh and none of this is meant to be any kind of reprimand. Sharing my perspective, hopefully giving you some traction to continue your reasoning and a whole lot of refusal to skip to some kind of solution or finished answer. My perspective is quite different from yours so there’s a bit of a collision and it may or may not appear harsh


#13

okay appreciate you help and i’ll continue to ensure that I acquire the knowledge that I’m seeking. Thanks


#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.