Getting Started with JavaScript
Getting Started with JavaScript
Introduction to Javascript
Console
The console
is a panel that displays important messages - errors and stuff like that.
If you use console.log()
then anything inside the parenthesis gets printed (logged) to the console. So this will print 42:
console.log(42);
Comments
You can do comments like this:
You can also do stuff like this:
Or you can do a multi-line comment thing:
You can even interrupt your own code with a comment!
Data Types
These can be:
Numbers (1, 2, 3, 4, 5...and they do NOT need to be encapsulated in quotes. Even if you do like
10.25
it doesn't need quotes)Strings (a group of characters surrounded by single quotes or double quotes)
Boolean (true or false)
Null (the absence of a value)
Undefined (noted with keyword
undefined
and is kind of like null but has different use)Symbol (unique identifiers. Won't worry about these for now)
Objects (the first 6 things in this list are primitive (basic) data types, and objects are more complex)
Arithmetic Operators
You can do addition, multiplication, subtraction, division with the +, -, /, *
signs. The remainder is %
.
So you can do stuff like:
String Concatenation
When '+' is used on two strings, it appends left string to right string. Like this:
Properties
When you introduce a piece of data into JS program, the browser saves it as an instance of the data type. For example, the property of length
contains the amount of characters in the string. You can use the dot operator
plus the property you want to get its value.
For example, this will give a result of 2:
Methods
Methods are actions you can perform. You can "call" them by using the dot operator
plus the name of the method. Like this:
Just don't forget that the method gets a '()' (I think) right after you use it. Check this example:
Built-in objects
An example is the 'math' object, and they rule cuz' they also have methods! So you can do something like this, which will create a random number between 0-1:
If you start generating random numbers, and multiplying them and other fun stuff like that, you might end up with weird decimals and stuff. So you can use a method called Math.floor()
which rounds down to the nearest whole number.
Here's a tricky one:
This uses Math.floor
to make the output of Math.random * 200
into a whole number.
Variables
Variables are like this:
The way that myName
is capitalized is known as camel casing
.
Keep in mind that variables:
Can't start with Numbers
Are case sensitive
Should probably not have two with similar capitalization - like
myName
andMyName
Once you set a variable, it's easy to print with:
Create a Variable: let
The let
keyword means a variable can be reassigned a different value. Like this:
You can also set a variable as nothing initially and then change it over time, like:
Create a Variable: const
const
is kind of like a var
but can't be changed. Check this out:
If you run this you'll get an error saying the const
has already been declared.
You need to give const
a value. You can't just leave it empty like you can with let
.
Mathematical Assignment Operators
With mathematical assignment operators, you can mess with the value of a variable like so:
In the above example:
The Increment and Decrement Operator
The variablename++
or variablename--
can increase or decrease the value of a stored value. For example:
Console log says:
String Concatenation with Variables
We've been adding strings to variables, and the +
operator can combine two string values even if they are being stored in variables. For example, I can the value of my favoriteAnimal
and then use it in a sentence:
String Interpolation
Interpolate is another word for insert. With ES6 of JavaScript we can interpolate strings using template literals. Here's an example:
In my opinion, it's way easier to use these template literals because the code is easier to read.
typeof operator
typeof
operators help you figure out what type of thing a variable is. For example:
And the output will say:
Last updated
Was this helpful?