Some Simple Questions from a Complete Beginner

So this means that they’re both identical in their returns, they both return the primitive Number 3 although the way to get to that return is different.

return a < b (yes i know that’s not the example you gave) will also return a primitive - in this case we don’t actually know what the value of the primitive will be (because it depends on the values of a and b), but we know that the type of primitive returned will be a Boolean - it will either return true or false .```

I though in this case that the return would be undefined, not a Boolean.

Nope.

One of two things is true:
1: The value stored in a is less than the value stored in b. Return value is Boolean true.
2: The value stored in a is not less than the value stored in b. Return value is Boolean false.

(And for the pedantic that follow, ‘value’ in this case varies based on what a and b are)

function altb(a,b) {
  return a < b;
}
altb(1,3) #true
altb(3,3) #false

But in the instructions I’m told:

Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined .

EDIT:
Also how come:

ints, which store whole numbers
doubles, which store bigger whole numbers and decimal numbers
booleans, which store true and false
chars, which store single characters using single quotes.
Strings, which store multiple characters using double quotes.

How come String uses a capital letter first as opposed to the others which are in small caps?
EDIT2:
And also, what does void mean here?

public class MyProfile {
	public static void main(String[] args) {   

	}
}

undefined is a primitive, and you can choose to return it, the same way i chose to return the Number 3 in my first example:
return undefined
an (in)equality will always evaluate to being a Boolean. But what you return is up to you.

Well lets first address something:
ints, doubles, and floats are all kinds of numbers. If you read the Primitive MDN page linked to earlier, there are 7 Primitive types: string,number,bigint,boolean,null,undefined, and symbol.

String is differentiated from string. A string is a primitive; a String is an object. Javascript will seamlessly coerce between the two as required for the operation it’s trying to perform; this is why "abc".length is a valid statement, despite primitives having no methods, and why you can do things like use array indexing on a String.

the same is true of Number and number, Boolean and boolean (though the Boolean object doesn’t really do much out of the box.)

1 Like

This isn’t Javascript. But void in this case means “I will return nothing; do not expect a value out of me.”

1 Like

From the code that was posted, it looks like you’re dealing with Java, which is a very different and incompatible thing from JavaScript.

1 Like

Oh my! I knew something was wrong. I clicked the wrong lesson! Well, it’s not lost. I did learn quite a lot of things there as well. I was thinking to myself it was familiar yet different.

Java is to Javascript as Pen is to Penguin.

Just to be clear:

image

The difference between a regular parameter and a default parameter is that with a default parameter we us the operator = to indicate what the default parameter is while if were just regular parameter we wouldn’t. Am I right?

Let’s give the return function a go again as I still don’t get it.

When a function is called, the computer will run through the function’s code and evaluate the result of calling the function. By default that resulting value is undefined .

This is from the exercise website I’m using at the moment. Why would it be undefined?

function rectangleArea(width, height) {
  let area = width * height 
}
console.log(rectangleArea(5, 7)) // Prints undefined

Isn’t 5*7 the result we should be getting?

What do you get in the console with this?

function rectangleArea(width, height) {
  let area = width * height; 
  return;
}
console.log(rectangleArea(5, 7));
1 Like

You haven’t returned anything from the function, so the default value of that function is undefined.

You can change that by returning something from the function, such as the area.

1 Like

I can’t run it as part of the exercise.

Ok so let me get this straight.

return means assigning to a function the result of the parameters. Am I right?

So for example this:

function monitorCount(rows,columns){
  
}

Is a function which would be undefined because I haven’t told it to store the result of the function to a variable. I have some parameters: rows and columns, but they’re just categories, they’re not precise values. On their own they can’t give any results.

function monitorCount(rows,columns){
  return (rows*columns)
}

In the code above, I’ve specified the result I wanted returned but I didn’t assign it to a variable.

function monitorCount(rows,columns){
  return (rows*columns)
}
const numOfMonitors = monitorCount(4,5);

I finally understood the difference between declaring a function and calling it which makes it a lot easier to understand.

So here and declared a new variable which equals to the function I just declared before.

Am I right?

… eh… kind of?

return means ‘whatever called me, hand it this value. I’m done.’

a given function could return different types of values in Javascript. Most commonly, this is used to indicate a failure (“I expected this to give me an array, but it gave me false. Something went wrong.”)

1 Like

Arrow functions remove the need to type out the keyword function every time you need to create a function.

Does this apply to every single function instance or just the helper functions?

So I restarted the lessons for the second time and this is where I got stuck last time.

So many questions:
image
I’m not sure I understand this one. I know the => is a function.

Why is a variable (let color) within the code body of another variable (const logSkyColor). I’m confused about this. Could someone clarity and make this as simple as possible to understand? Thank you!

The const and let declarations are a more advanced concept.

The var declaration defines a variable within function scope. Everywhere within the function can see that variable. If you’re not inside of a function then it defines it within global scope instead.

The const declaration defines a variable within block scope, where it can’t be seen outside of the block that it was defined. Also as it’s a constant, primitive types (such as number or string) can’t be changed, which is a good thing for reliability and is also good for the JavaScript interpreter. If the constant is an array or an object, its contents can still be changed.

The let declaration also defines a variable within block scope, but it’s contents can change. It’s usually best to avoid the let statement because that’s like saying “Warning! This will change soon!”, so the code that you had before is more appropriately written with a const declaration instead of the let one.

const logSkyColor = () => {
    const color = "blue";
    console.log(color); // blue
};

However, arrow notation is also usually best reserved for when using single liners, with more complex functions being defined using normal function notation instead.

When dealing with multiple statements, use a function declaration.

function logSkyColor () {
    const color = "blue";
    console.log(color); // blue
}

That variable assignment doesn’t need to be there with such a simple function.

function logSkyColor () {
    console.log("blue");
}

And because there’s just one statement in the function, that’s a good time to use arrow-notation instead.

const logSkyColor = () => console.log("blue");
1 Like

Almost like it’s the definition of a variable :wink:

You shouldn’t avoid let. You should use it appropriately.

In this specific example, let would be unnecessary.

2 Likes

Yes, avoid is too strong a word. Better is to say that it is preferred to use const instead of let.

1 Like