FAQ: Operators - Using Operators to Make Comparisons

A greater than (>) comparison returns true if the value on the left is greater than the value on the right of the operator.

oranges = 3
bananas = 5

3 > 5 ?  no

so, false.

1 Like

The one thing that I don’t get is how would the program know the weight, did we put it somewhere in a variable early on in a program? I’m sorry if I’m being an idiot.
strawberry_weight = ?
is (strawberry_weight == .5lb)? => true

The example assumes there is a variable in the program, but that is nothing to do with the example in the editor window. It is simply saying,

if (some_variable has the value 'some_value') then we can resolve it to `true`

It is a very common construct most programs since it is how we deal with much of the logic. if state then outcome.

1 Like

I don’t understand about the comparing two known values oranges and bananas.

bananas = 5
oranges = 3

is (oranges > bananas)? => false

Why are we doing this? Surely we already know that “there are more oranges than bananas is false” because we know that oranges is 3 and bananas is 5. So we know oranges is less than bananas and we need to add more oranges, because we know how many bananas and oranges we have. Why are we asking the computer something we already know? Or is there something we don’t know that i’m not seeing?

How can we know what the values are if they are dynamically computed? We have to give the computer a way to inspect and make decisions based on those unknown quantities.

I’m soooooooo confused! its my first time learning this. This is my first course and I’m very very far. I need help but I do not know how to ask for help.

what is a triple equal sign for?

=== is the JavaScript operator for identity, namely for testing if two operands are exactly identical (in both value and type). There is another operator, == which is for equality, namely, two values look alike even though they may be of differing type.

'1' == 1     //  true

'1' === 1    //  false
2 Likes

thanks! that makes sense

1 Like

Hi all, I’m looking for some help to see if I am reading this code correctly. I have a few questions in this post so please bear with me

strawberry_weight = ?
is (strawberry_weight == .5lb)? => true

Here is my interpretation.

? indicates an unknown individual strawberry weight.
== used to compare total strawberry_weight to see if it is the same as the value we are seeking, being 5lb.
Does the ? after close parenthesis relate to the unknown weight in the variable above?
=> Does this mean equal or greater than?

Thanks for any assistance in coming to grips with this steep learning curve.

We can stipulate that the ? stirred the right kind of attention, and you did, in fact, answer your own question. The question mark was not code, just a placeholder on evaluation of input. Bottom line, there is no syntax that supports ‘?’ in JS, apart from ternary expressions, which does not apply here.

strawberry_weight = ?

means we are waiting for user input in pseudo code terms.

strawberry_weight = input('strawberry weight: ')

Now we have a value that we can look into further. User input.

There are a number of ways we can evaluate this input, but confining it to below, on, or above a set value is a suitable place to start. The example is centered around weights, so let’s give it a go, (w = strawberry_weight)

o = 0.5
if w > o
if w == o
if w < o

Where in that test sequence do we test for greater than or equal to or for less than or equal to?

There is no sequence that tests for greater than or equal to. I was only inquiring about the => in the below, as it was the only thing I could think of that this combination of symbols would mean. Sounds like it’s just an arrow, and I’m reading too much into this exercise.

strawberry_weight = ?
is (strawberry_weight == .5lb)? => true

The question mark is just that; it’s asking us to imagine some numeric value (in this case) on the right side of the assignment operator (=). It is not part of the code, only the narrative.

In JavaScript, ? is the ternary operator, only used in ternary expressions:

action = w === 0.5 ? 'allow' : 'discard';

If ternary has not come up in the lessons yet, this can be ignored. Just know that it is the only usage of the ? symbol in JS.

As for => that is also used in the above context as part of the narrative to indicate an outcome. It is not a code symbol in this instance. However, like the ternary operator, it does have a role in function syntax.

Like the ternary, there is only one usage case in practical code:

Given a standard function expression:

const foo = function (x) {
    return x;
}

we can simplify this form with an arrow function:

const foo = x => x;

Both are forms of the function expression, and both exhibit the same behavior.

Bottom line, sometimes we may see symbols in pseudo code and narrative and need not interpret them as code. There is another hint that the above is narrative, and not code:

0.5lb

lb is the abbreviated form (symbolic of) ‘pound weight measure’, and used to represent weight unit, as in,

1 kg equals to 2.2 lb

There are no units in JavaScript. If we wish to express units it will have to be written into our code as part of our output string.

constant kilogram_to_pound = kg => kg * 2.2

console.log(`5 kg equates to ${kilogram_to_pound(5)} lb weight.`)

// 5 kg equates to 11 lb weight.

One last hint that the above is narrative: JS has no keyword, is.

Does each programming language have its own little ‘quirks’ or ways of showing what is happening?

i.e., Equal to something, some use ==; some use ===; math uses =