Introduction To Javascript
Introduction To Javascript
What is JavaScript?
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.
1. A single line comment will comment out a single line and is denoted with two
forward slashes // preceding it.
console.log(5); // Prints 5
2. A multi-line comment will comment out multiple lines and is denoted
with /* to begin the comment, and */ to end the comment.
/*
This is all commented
console.log(10);
None of this is going to run!
console.log(99);
*/
You can also use this syntax to comment something out in the middle of a line
of code:
Data Types
Data types are the classifications we give to the different kinds of data that we use in
programming. In JavaScript, there are seven fundamental data types:
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!
Next, we printed the number 40, notice we did not use quotes.
Arithmetic Operators
Basic arithmetic often comes in handy when programming.
An operator is a character that performs a task in our code. JavaScript has several
built-in in arithmetic operators, that allow us to perform mathematical calculations on
numbers. These include the following operators and their corresponding symbols:
1. Add: +
2. Subtract: -
3. Multiply: *
4. Divide: /
5. Remainder: %
The first four work how you might guess:
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: