FAQ: Introduction to JavaScript - Built-in Objects

Math is a built-in object that works with the Number type. Like all the built-ins, it is capitalized. console has no bindings to the global object. We can only access it through the browser Developer Tools menu.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

https://developer.mozilla.org/en-US/docs/Web/API/console

There may be some ambiguity, here, owing that I’ve never been down the Window.console road. Window.console property returns a reference to the Console object. What this means for us is still in question.

https://developer.mozilla.org/en-US/docs/Web/API/Window/console

1 Like

Why are these objects case sensitive to inputs
eg

console.log(Math.random()); - gives correct results
but
if console.log(math.Random()); —gives error----why

Insights plzz

The Math object is a global object that is available in javascript. Just like any other object it is saved in a variable. The variable is Math. Just as when you assign a variable name it is case sensitive. The methods assigned are also case sensitive.

JavaScript is case sensitive.

math

and,

Math

are worlds apart.

As the course progresses we learn how to create custom objects called Classes. The convention in JS is to capitalize class names, which is why Math is capitalized. It is a class. The Math class. The only difference between it and the classes we will build is that it is not a constructor and has only static methods. We learn all about this later on.

We can find all the properties of the class in the documentation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

The exercise says that console is an object built into JavaScript, but console is not on the list of objects on the page they link to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

Why?

console is not a global object but it can be accessed from any global object.

The Window.console property returns a reference to the Console object, which provides methods for logging information to the browser’s console. These methods are intended for debugging purposes only and should not be relied on for presenting information to end users

As to ‘why?’ the operative phrase above is, ‘intended for debugging’.

This may seem like a stupid question but how do we find out what methods are contained in an object without using the internet (as you may not always have access to it but can still write code)? Ive done the basic c++ course codecademy and I always meant to ask how you know what is contained in a specific library without using the internet?
Andy

1 Like

4 posts were split to a new topic: Does that mean it’s never going to be 1?

are objects and libraries the same thing? Their definitions in this module are the same.

Libraries are also objects, which is what pretty much everything is in JS. It goes a little deeper, though. Modules are object literals, and some libraries, such as lodash.js are also object literals, but many are function objects, such as jQuery.js.

1 Like

I realized this too. With exactly that section. I think what this whole course does is let you pass to the next section after getting it wrong twice… i think… because my second attempt to that was accepted as correct and it was slightly different from what you got… yet… when i restarted the section and just let the hints guide me, i saw that the correct answer is something different ENTIRELY. The hint told me to look for “Math.ceil(0)” which is defined as “Returns the smallest integer greater than or equal to x”.

Now, because they are fundamentally different from the combination of math floor and math random it took me 30 minutes to even realize that i was supposed to use something else. sigh This has happened on literally ALL coding tutorials. It’s like ALL CODING TUTORIALS don’t understand that because this is not math, that we can’t just assume things because we are literally learning new logic (because we are learning what different Methods are and what they do) so we need to be told EXACTLY when we are going to switch to a new method because most of us are still just following instructions and have NO INTUITION for this yet. At least I don’t, and I am the target audience for this because i have 0 coding knowledge.

This is super frustrating especially seeing that i guess this is a flaw that ALL CODERS and CODING… INSTITUTIONS(?) have that they don’t know how to teach people that truly don’t know anything about code.

2 Likes

whare do I put .ceil() in this??
console.log(Math.floor(Math.random()) * 100))

actually I found it,

1 Like

Mathematical question: other than 0 or 50 themselves, I do not understand how any number between 0 and 50 is produced from the following code:

Math.random() * 50;

If the result of Math.random() is either 0 or 1, then surely multiplying 0 * 50 = 0, and 1 * 50 = 50 - so it should only give those two outcomes, right? like, aren’t the possibilities for that code:
a) Math.random() → 0 → 0 * 50 → 0
b) Math.random() → 1 → 1 * 50 → 50

can anyone help? i’m a complete beginner at programming, and still working on my maths skills, so if anyone could explain it as if i’m a baby, then i’d really appreciate it :slight_smile:

Will, in fact, generate a float between 0, inclusive, and 49.999..., up to but not including 50.

To get integers, we need to floor the generated float. It will round down to the nearest integer, so we have {0..49} as our solution set.

Math.floor(Math.random() * 50)

If we want both 0 and 50, then we can use Math.ceil() to get the ceiling (round up to the nearest integer). The ceiling of zero is zero so it does not round up to 1. That wouldn’t be rounding, but addition.

The most common form that does include 50 but not zero is the offset.

Math.floor(Math.random() * 50 + 1)

Whether we perform the addition inside the call to Math.floor or outside, the result is the same. We’re either adding 1 to 49.999 and rounding down; or rounding down 49.999 to 49 then adding 1 to get 50. In either case, 0 will not be in the solution set.

I am stuck in making .ceil function can someone please help me ?

3 Likes

Why does the lesson state that console is a built-in JavaScript object while it is not? console is part of a browser/node API, but it’s NOT part of JavaScript.

In addition to console , there are other objects built into JavaScript.

Even if you follow the provided link you don’t find console there :man_shrugging:

However, it is,

injected to JavaScript to be accessible to JS code on the browser.
ecmascript 6 - JavaScript: Why isn't console considered a standard built-in object? - Stack Overflow

As far as the lesson’s accuracy is concerned, perhaps the author is deliberately over-simplifying for the benefit of learners who have yet to develop a technical sense. Sometimes over explaining is tantamount to overload and the whole lesson is lost.

2 Likes

Built in functions are there for our convenience and follow along lines of common purposes. There is no assuming, only assessing requirements of our code design. It is us who set the criteria.

Math.random() only generates a float (a decimal fraction) less than 1 and equal to or greater than 0. When we multiply that by a whole number we still have a float. If our requirement is a whole number then we must reach into the tool box for another tool… Math.floor() or Math.ceil(). One will give us the small integer, by truncating, the other will give the larger integer by rounding up to it.

As far as generating random integers, we only need to know the upper bound of the range. For a die with six sides, the upper bound is 6.

Math.random() * 6

will give us a number between 0, inclusive, and 6, exclusive. To make that into a whole number from 1 to 6 inclusive, we floor the value then add 1.

These are not assumptions but action steps that we design into our program. Put the idea of assuming anything right out of your mind and gain familiarity with the built in functions as they are presented. Explore and learn. This is how you build knowledge and confidence.

A lot of the learning process involves experimenting with different ideas. Form a hypothesis (a guess, essentially) of an expected outcome based on our experience, write the code and test it. If the outcomes don’t match our expectations then it is up to us to resolve the reason why.

Code is built around promises. A given built in promises a certain outcome and we can count on that outcome (or range of outcomes) every time. Our code must operate on the same premise. We promise the user a certain outcome or result and our code must deliver upon that promise every time, or have a good fall back in the event it does not.

Here is a famous lecture by the late Professor Richard Feynman:

Richard Feynman Teaches you the Scientific Method - Farnam Street

Ultimately it is our responsibility to work toward removing of guess work and replacing it with functional code that works because we know it will. Keep at it. If you must guess, then test, test, test and don’t be disappointed if you guessed wrong. By process of elimination, that is one guess you can check off.

Hi! In one of the exercises, I followed the example of one of the users. He tried to print to the console the result of Math.ceil, but at the same time, evaluate (and print) that the number is an integer. How can we achieve this?

the code I used was this:
console.log(Number.isInteger(Math.ceil(2.8)));

Slightly different code but similar to him, I want to print the number on the console, but also print the boolean value, true or false. When I run it, it only shows the ‘true’ value but not the number (which should be 3).

How can we print both values so as they show at the same time? Apologies for my bad English.

This is kind of unrelated to this topic, but does JavaScript not have an autocomplete feature, or is it just not available in app.js? The only other language I know is MATLab, and in Live Editor there’s autocomplete for methods, arguments, and properties.

Apologies if any of the vocab I’ve used is out of context, I’m still getting oriented with all of it.