JavaScriptLectureCodecademy
JavaScriptLectureCodecademy
Last year, millions of learners from our community started with JavaScript.
Why? JavaScript is primarily known as the language of most modern web
browsers, and its early quirks gave it a bit of a bad reputation. However, the
language has continued to evolve and improve. JavaScript is a powerful,
flexible, and fast programming language now being used for increasingly
complex web development and beyond!
Since JavaScript remains at the core of web development, it’s often the first
language learned by self-taught coders eager to learn and build. We’re excited
for what you’ll be able to create with the JavaScript foundation you gain here.
JavaScript powers the dynamic behavior on most websites, including this one.
In this lesson, you will learn introductory coding concepts including data types
and built-in objects—essential knowledge for all aspiring developers. Make
sure to take notes and pace yourself. This foundation will set you up for
understanding the more complex concepts you’ll encounter later.
Console
The console is a panel that displays important messages, like errors, for
developers. Much of the work the computer does with our code is invisible to
us by default. If we want to see things appear on our screen, we can print,
or log, to our console directly.
It’s going to be very useful for us to print values to the console, so we can see
the work that we’re doing.
console.log(5);
This example logs 5 to the console. The semicolon denotes the end of the line,
or statement. Although in JavaScript your code will usually run as intended
without a semicolon, we recommend learning the habit of ending each
statement with a semicolon so you never leave one out in the few instances
when they are required.
You’ll see later on that we can use console.log() to print different kinds of
data.
Comments
Programming is often highly collaborative. In addition, our own code can
quickly become difficult to understand when we return to it— sometimes only
an hour later! For these reasons, it’s often useful to leave notes in our code for
other developers or ourselves.
As we write JavaScript, we can write comments in our code that the computer
will ignore as our program runs. These comments exist just for human readers.
Comments can explain what the code is doing, leave instructions for
developers using the code, or add any other useful annotations.
.
A single line comment will comment out a single line and is denoted
with two forward slashes // preceding it.
.
.
.
.
You can also use a single line comment to comment after a line of
code:
.
.
console.log(5); // Prints 5
.
.
.
.
.
.
.
You can also use this syntax to comment something out in the middle
of a line of code:
.
.
The first 6 of those types are considered primitive data types. They are the
most basic data types in the language. Objects are more complex, and you’ll
learn much more about them as you progress through JavaScript. At first,
seven types may not seem like that many, but soon you’ll observe the world
opens with possibilities once you start leveraging each one. As you learn more
about objects, you’ll be able to create complex collections of data.
But before we do that, let’s get comfortable with strings and numbers!
In the example above, we first printed a string. Our string isn’t just a single
word; it includes both capital and lowercase letters, spaces, and punctuation.
Next, we printed the number 40, notice we did not use quotes.
Arithmetic Operators
Basic arithmetic often comes in handy when programming.
. Add: +
. Subtract: -
. Multiply: *
. Divide: /
. Remainder: %
The first four work how you might guess:
Note that when we console.log() the computer will evaluate the expression
inside the parentheses and print that result to the console. If we wanted to
print the characters 3 + 4, we would wrap them in quotes and print them as a
string.
The remainder operator, sometimes called modulo, returns the number that
remains after the right-hand number divides into the left-hand number as
many times as it evenly can: 11 % 3 equals 2 because 3 fits into 11 three times,
leaving 2 as the remainder.
String Concatenation
Operators aren’t just for numbers! When a + operator is used on two strings, it
appends the right string to the left string:
Just like with regular math, we can combine, or chain, our operations to get a
final result:
console.log('One' + ', ' + 'two' + ', ' + 'three!'); // Prints 'One, two,
three!'
Properties
When you introduce a new piece of data into a JavaScript program,
the browser saves it as an instance of the data type. Every string
instance has a property called length that stores the number of
characters in that string. You can retrieve property information by
appending the string with a period and the property name:
console.log('Hello'.length); // Prints 5
Methods
Remember that methods are actions we can perform. JavaScript provides a
number of string methods.
console.log('hello'.toUpperCase()); // Prints
'HELLO'console.log('Hey'.startsWith('H')); // Prints true
You can find a list of built-in string methods in the JavaScript documentation.
Developers use documentation as a reference tool. It describes JavaScript’s
keywords, methods, and syntax.
Built-in Objects
In addition to console, there are other objects built into JavaScript. Down the
line, you’ll build your own objects, but for now these “built-in” objects are full
of useful functionality.
To generate a random number between 0 and 50, we could multiply this result
by 50, like so:
Math.random() * 50;
The example above will likely evaluate to a decimal. To ensure the answer is a
whole number, we can take advantage of another useful Math method
called Math.floor().
Math.floor(Math.random() * 50);
In this case:
To see all of the properties and methods on the Math object, take a look at the
documentation here.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo
bal_Objects/Math
Review
Let’s take one more glance at the concepts we just learned:
Download the Introduction: Cheat Sheet to help you remember the content
covered in this lesson.
https://www.codecademy.com/learn/introduction-to-javascript/modules/l
earn-javascript-introduction/reference
Variables
In programming, a variable is a container for a value. You can think of
variables as little containers for information that live in a computer’s memory.
Information stored in variables, such as a username, account number, or even
personalized greeting can then be found in memory.
Variables also provide a way of labeling data with a descriptive name, so our
programs can be understood more clearly by the reader and ourselves.
In short, variables label and store data in memory. There are only a few things
you can do with variables:
It is important to distinguish that variables are not values; they contain values
and represent them with a name. Observe the diagram with the colored boxes.
Each box represents variables; the values are represented by the content, and
the name is represented with the label.
In this lesson, we will cover how to use the var, let, and const keywords to
create variables
Create a Variable: var
There were a lot of changes introduced in the ES6 version of JavaScript in 2015.
One of the biggest changes was two new keywords, let and const, to create,
or declare, variables. Prior to the ES6, programmers could only use
the var keyword to declare variables.
. var, short for variable, is a JavaScript keyword that creates, or declares, a new
variable.
. myName is the variable’s name. Capitalizing in this way is a standard convention in
JavaScript called camel casing. In camel casing you group words into one, the first
word is lowercase, then every word that follows will have its first letter uppercased.
(e.g. camelCaseEverything).
. = is the assignment operator. It assigns the value ('Arya') to the variable (myName).
. 'Arya' is the value assigned (=) to the variable myName. You can also say that
the myName variable is initialized with a value of 'Arya'.
. After the variable is declared, the string value 'Arya' is printed to the console by
referencing the variable name: console.log(myName).
Note: In the next exercises, we will learn why ES6’s let and const are the
preferred variable keywords by many programmers. Because there is still a ton
of code written prior to ES6, it’s helpful to be familiar with the
pre-ES6 var keyword.
If you want to learn more about var and the quirks associated with it, check
out the MDN var documentation.
Another concept that we should be aware of when using let (and even var) is
that we can declare a variable without assigning the variable a value. In such a
case, the variable will be automatically initialized with a value of undefined:
If you’re trying to decide between which keyword to use, let or const, think
about whether you’ll need to reassign the variable later on. If you do need to
reassign the variable use let, otherwise, use const.
let w = 4;w = w + 1;
console.log(w); // Output: 5
In the example above, we created the variable w with the number 4 assigned to
it. The following line, w = w + 1, increases the value of w from 4 to 5.
Another way we could have reassigned w after performing some mathematical
operation on it is to use built-in mathematical assignment operators. We
could re-write the code above to be:
let w = 4;w += 1;
console.log(w); // Output: 5
The increment operator will increase the value of the variable by 1. The
decrement operator will decrease the value of the variable by 1. For example:
Just like the previous mathematical assignment operators (+=, -=, *=, /=), the
variable’s value is updated and assigned as the new value of that variable
Notice that:
typeof operator
While writing code, it can be useful to keep track of the data types of
the variables in your program. If you need to check the data type of a
variable’s value, you can use the typeof operator.
Review Variables
Nice work! This lesson introduced you to variables, a powerful
concept you will use in all your future programming endeavors.
Instructions
console.log(test1);
NTRODUCTION TO JAVASCRIPT
Kelvin Weather
Deep in his mountain-side meteorology lab, the mad scientist Kelvin
has mastered weather prediction.