# Intro to JavaScript
## Problem Statement
If we want to create modern, fully-equipped websites and web apps, we need to
use JavaScript. This programming language has its own uses, history, tools and
practices, and becoming familiar with them will let us leverage JavaScript's
power to improve all of our web experiences.
## Objectives
1. Describe the JavaScript language and its uses
2. Outline the history of JavaScript
3. Write JavaScript in the Chrome Dev Tools console
4. Identify JavaScript syntax and best practices
5. Establish good JavaScript learning habits
## Describe the JavaScript language and Its Uses
_JavaScript_, commonly abbreviated as _JS_, is the programming language of the web.
Along with HTML and CSS, it's used to create virtually all the web pages and
apps you interact with, from Google to Facebook to Wikipedia to Amazon to
Netflix.
Each of these three main web technologies plays a role in creating the content you
see on a website:
- **HyperText Markup Language (HTML)** provides structure to the content, arranging it in a nested, tree-like way, and allows us to mark up the content with attributes like `id` and `class`.
- **Cascading Style Sheets (CSS)** adds styling to the page, letting us customize the look of the content, often using `id` and `class` attributes, in addition to the semantic elements, to target the elements we want to style.
- **JavaScript** does pretty much everything else and handles most of the dynamic behavior of a website. For example: reacting to user events (like clicking a button), changing the page's content without reloading, and executing network requests in the background. Like CSS, the joining of HTML elements and changes that happen when they're interacted with (clicked on, moused over, etc.) often happens by targeting elements by their `id` or `class` attribute.
When you hover your mouse over a button and it wiggles in anticipation, or when
you see new notification alerts on Twitter without refreshing the page, or when
new content progressively appears at the bottom of the page as you scroll down
your Facebook news feed—that's all JavaScript. At this point, JavaScript is a
fundamental aspect of most users' web experience.
JavaScript is one of the most popular languages in the world, and it shows no
signs of slowing up. It's still the only programming language that can run in
every major browser (HTML is a _markup language_ and CSS is a _style sheet
language_).
A language of many talents, JavaScript allows us to create incredibly diverse
applications that run in the browser, on the back-end as NodeJS, on the desktop, and on
mobile devices. It's a flexible language, allowing us to program in many
different styles — [functional][fn] and [object-oriented][oo], in particular — mixing and
matching concepts to adapt to our needs. It's powerful, scalable, and has a
robust community of developers churning out new code and advancing the language.
And, finally, it's a great first or second language to learn.
## Outline the History of JavaScript
The first version of JavaScript was built incredibly quickly in 1995. There were
a few other competitors vying to become the language of the web, but they never
gained enough traction to knock off JavaScript. And then... not much happened.
There were incremental improvements and adjustments, but JavaScript development
around 2000 very much resembled JavaScript development in the mid-1990s.
### The Emergence of Asynchronous JavaScript
Around the turn of the century, Microsoft wanted to give its customers a way to
access their email via a web browser, so they added a new JavaScript feature to
Internet Explorer that allowed an already-loaded web page to fire off a request
for additional data. It was a huge improvement over existing webmail services
that required the whole page to be reloaded in order to check for new email. The
process of making requests for additional data became known as _Asynchronous
JavaScript and XML_, or _AJAX_. (We'll take a long look at AJAX and asynchronous
code execution later on in the JavaScript material.) By 2004, when Gmail came
along with a heavy dependence on AJAX, JavaScript was in the early stages of a
rise in popularity and usefulness that has continued to the present day.
### Standardization and Modernization
Even though AJAX provided a lot of useful functionality to the language,
JavaScript was still difficult to program. Different browsers implemented
different features in different ways. There were a few competing camps which all
had different ideas about what the language should be. In early 2009, everyone
came together to agree on standardization. The fifth version of JavaScript was
released in late 2009. It's known as _ES5_ ('ES' for _ECMAScript_, the official
name of the JavaScript specification) or _Harmony_ (because it harmonized
competing implementations of the ECMAScript specification). (Note: We still
refer to the language as JavaScript, and use the particular version titles only
when we need to point to particular version features.)
Harmonization was great, but the language was still lacking modern language
features common to most standard programming languages like PHP, Java, C, Python
and Ruby. JavaScript began to address those concerns in 2015 with the unveiling
of _ES6_ or _ES2015_, the sixth version of JavaScript. Since then, they have
released updates every year, including _ES2016_ and _ES2017_, both of which have
brought additional modern features into the language.
## Compilation and JavaScript
The name _JavaScript_ was chosen as a marketing ploy: the _Java_ language was
all the rage in 1995. The two languages share very little in common. Some JS
syntax is borrowed from Java (to make it more familiar for those early
developers coming over from the world of Java), but that's about it.
Unlike Java, JavaScript code doesn't need to be compiled. The code that you
write in a JavaScript file is the exact same code that the web browser parses
and runs. In a compiled language like Java, you write some code in a human-
readable format, and then the compiler takes that code and essentially
translates it into machine-readable code. That means that it's very difficult to
open up the source code for an application written in a compiled language and
poke around in it to understand what's going on.
Because JavaScript doesn't get compiled, that is exactly what we can do with
JavaScript code. Towards the end of this JavaScript curriculum, you'll find
yourself able to go to a website you like, open up the source code using your
browser's developer tools, and look directly at all of the JavaScript code used
on that site. This is a powerful feature of the language and an incredible tool
for learning by example.
## Write JavaScript in the Chrome Dev Tools Console
Every major browser comes with a built-in set of developer tools that you can
use to inspect, modify, and debug the content of a web page.
***NOTE:*** To ensure that instructions and screenshots match up with your
experience, use [Google Chrome](https://www.google.com/chrome/index.html) browser.
To [open the dev tools in Chrome](https://developers.google.com/web/tools/chrome-devtools/console/#open_as_panel
), press `Ctrl+Shift+J` (Windows / Linux) or `Cmd+Opt+J` (Mac). Chrome ships with a whole suite of useful dev tools, but the only one we care about for now is the JavaScript console.
The console is an environment in the browser where we can type and run arbitrary
JavaScript code in the context of the current browser window. The console is
_sandboxed_, meaning the only resources it has access to are those loaded on the
current page. Once we start declaring variables and functions in separate
JavaScript files, we'll be able to access and play around with them in the
console. The console is the single best tool for debugging JavaScript in the
browser, so start familiarizing yourself with it now.
The `Ctrl+Shift+J` / `Cmd+Opt+J` command should open up straight into the
console. If, for whatever reason, it doesn't, you can always click on `Console`
in the dropdown (when the dev tools are collapsed) or in the list of tabs:
### Debugging
JavaScript comes with a number of built-in features that can help us build and
debug our applications. One of the simplest is the `alert()` function, which
creates a simple alert in the browser window:
View Intro to JavaScript on Learn.co and start learning to code for free.
[fn]: https://hackernoon.com/functional-programming-what-language-should-you-be-talking-313dd8bc379b [oo]: https://en.wikipedia.org/wiki/Object-oriented_programming