Predefined
Predefined functions are built into the JavaScript language and made readily available for us to use. Let's use a few to get the idea.
File Location
We will be working in the following file:
Predefined Examples
Here are a few examples of predefined functions used commonly in JavaScript:
Name
Purpose
isNaN()
Determines if a value is 'Not a Number'
parseInt()
parses a string argument and returns an int
isNaN()
isNan()
is a pre-defined function that checks to see if something is not a number. Let's practice using it by copying the following code. After you copy the code, try to guess what will happen with 1 & 2 in the results.
parseInt()
Another commonly used predefined function is parseInt()
. This method is used when you want to convert a string number "6"
to an integer 6
. Consider the function below:
Blending the two
If we're taking input from a user in the form of a string and we want them to enter numbers, what about checking to be sure they enter a number. For instance, what if a user enters XYZ, instead of an actual number. We would need to check to be sure that they have entered a number. Let's take that same function and check to make sure that we have parsed a number. Consider the additional logic below. See if you can answer the question of what happens when we print numberThree.
Additonal Practice
What you might want to do is go pick 3-5 of them and write some code that uses some of these functions.
Last updated