#newbie to javascript and already confused about console.log

10 messages · Page 1 of 1 (latest)

fallow bobcat
#

what im confused about is the idea that you can type anything in the brackets for console.log?
here's an example of what i mean (using the example in the frontend dev pathway):
function increment() {
console.log("The button was clicked")
}

from my understanding, the reason that anything can be written within the brackets of the console that is placed within the function's brackets, is because it's within the function's brackets? (sorry if this is confusing, it's hard to articulate my question)

mild owl
fallow bobcat
#

@mild owl thanks ^-^ will look now

marsh sigil
#

Ok, so there are some fundamentals that you need to understand... the first is the idea of a "function".

Firstly, a function is simply a re-usable bit of code... Say for example you needed to perform some calculation a bunch of times in your code. Instead of rewriting the same ~10 lines of code in a dozen placed you can instead write a function to perform the calculation and then "call" the function (invoke it) to perform the calculation in those dozen spots.

Next, a function can be WAY more useful if you can pass it some data to work with... So imagine a function that calculates the area of a square (I know that's trivial but bear with me). You COULD have a function which calculates the area of a 2"x2" square but it wouldn't work for a 3"x3" square. Wouldn't it be nice if you could write the function so that you could tell it how big a square? Enter "parameters" - the parameter allows you to pass some data to the function.

Now, "log" is a function of "Console" object. All it does is write <something> to the output window. You can pass a "parameter" to the function (as discussed above) telling it WHAT to output... It's very flexible and has a LOT of options but just think about it as a way to dump some useful debugging information to the output window.

fallow bobcat
#

@marsh sigil thank you so much for your help ^-^ reading through this now

#

so whatever is typed within the console.log() parenthesis is essentially just to help whoever is looking at the code understand what is going on?? sorry, im just grappling with this new concept, reading through the link sent as well

marsh sigil
#

example:

// this version is VERY specific...
function calculateAreaOf2x2Square() {
  const area = 2 * 2;
  return area;
}

// this version is WAY more useful
function calculateAreaOfSquare(lengthOfSide) {
  const area = lengthOfSide * lengthOfSide;
  return area;
}

//Now you can use the more useful version like so:
calculateAreaOfSquare(3);
calculateAreaOfSquare(4);
calculateAreaOfSquare(5);
marsh sigil
fallow bobcat
#

ahh okay okay

left peak
#

I think you should try YouTube