To use our site, you'll need a larger display.
Here are some of the most common data types in JavaScript:
true/false statement. It equates in binary as
0
or 1. Booleans are great for tracking things that can be answered in a yes or no
question, such as Is the user logged in.
-inf to inf.
You can convert a boolean or string with numbers only to a number with the parseInt();
function or the parseFloat(); function.
Take note that if you have a decimal value, you
may see some odd results at the end of the decimal if you add or subtract from it.
This is because
internally, JavaScript converts decimals to doubles in binary, and are known to have inaccuracies
when preforming calculations with them.
To counter this, you might want to round them (using
Math.round()).
toString(); function. Strings
must be wrapped in quotes.
JavaScript has a few ways to contain multiple types of data. The most common are listed below:
years['2'] instead of years[2]), although usually not necessary.let fruits = ["apple", "peaches", "pears"];console.log(person.inSchool)); you will get the
value of false. You can also create more properties after initialization by just
defining them as you would another variable. So if you wanted to add a property called gpa, you could just write person.gpa = 2.8;.
var x = 2;) There isn't too much of a reason to use the
var keyword anymore, as you can use the let or const keywords
instead for better access to variables.
let magicString = "Wanna see magic?";)const unchangeable = true;)function declaration or arrow syntax.This practice activity is designed to allow you to test out variables and functions. This should only take 5 minutes if you get the hang of it. There will be no answer key, but a solution will be available at the bottom of the page for you to view.
In the first section of the unit, you learned about basic JavaScript keywords and how to use them. Now we will use them in a basic JavaScript runtime. You will create a variable, change its value, and declare a constant. Then you will declare a function with two parameters and call it to log an output.
Make sure you have a new HTML document and JavaScript file linked to the HTML document.
Use these files as templates if you would like.
Create a variable using the let statement. Give it a creative name, assign it to 5,
and then use the console.log(variable); to log the value. The console should log the
value 5 when you run it.
After the console.log(); statement, use the variable name and then += 5. The line
should look like this: variable += 5;. Then add another console.log();
statement for the same variable.
Write a declaration for a constant named add_this, and assign it the value of 2.
Now we will create a function. Declare a function with the function keyword and give it the name
of incrementNumber. Add two parameter variables, initial and incrementor.
Then add a block statement ({ }). It should look like this:
Now we're going to edit the block statement to return a value to the user. Start with the return
keyword and then write initial + incrementor.
Now that we have created the function, we need to call it. Write the function name, and then place the
variable that you created at the start of this activity in the first parameter spot as the first argument,
and the constant add_this as the second argument. It should look like this, fill in the blanks:
console.log(incrementNumber(___, ___));
Please submit this activity per the direction of your teacher.
The solution's output is available below in our Fiddle editor. Use the button below to open the solution in the Fiddle Console.
You may need to open DevTools by pressing F12 or CTRL+SHIFT+I. You might also need
to sign out of your web browser if you can't open your DevTools.
JavaScript can preform math with built-in operators, + for addition, - for
subtraction, * for multiplication, / for division, ** for
exponents, and Math.sqrt() for finding square roots. There's more you can use the built-in math
library for, but that is for another day.
aka if...then...else
There's something important that allows your website to make its own decisions, conditionals (also known
as
if...then...else statements). These statements will check if a condition is met, and then execute some
commands if it is true. If it is not true, the browser will check for another if statement
and check it, and if it is true, run it. If the browser runs through all the if statements
in a conditionals block, and there's an else block, whatever is in there will be run.
You'll need an operator to check whether a statement is true or not, and that is where comparators come into play. The most common ones are listed below.
==!=><>=<====!==Sometimes you'll want to check for multiple conditions in one conditional statement. That's where AND as well as OR come in.
&&true if
all the conditions in the statement are true. Otherwise, it'll be false.
||true if at least one of
the conditions are true.