Skip to content

Merging wiki pages into main project. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@ We concentrate on the language itself here, with the minimum of environment-spec

### An Introduction

1.1 [An Introduction to JavaScript](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/1.1-An-Introduction-to-JavaScript)
1.1 [An Introduction to JavaScript](pages/1.1-An-Introduction-to-JavaScript.md)

1.2 [Code editors](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/1.2-Code-editors)
1.2 [Code editors](pages/1.2-Code-editors.md)

1.3 [Developer console](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/1.3-Developer-console)
1.3 [Developer console](pages/1.3-Developer-console.md)

### JavaScript Fundamentals

2.1 [Hello, world!](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/2.1-Hello,-world!)
2.1 [Hello, world!](pages/2.1-Hello-world.md)

2.2 [Code structure](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/2.2-Code-structure)
2.2 [Code structure](pages/2.2-Code-structure.md)

2.3 [The modern mode, "use strict"](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/2.3-The-modern-mode,-%22use-strict%22)
2.3 [The modern mode, "use strict"](pages/2.3-The-modern-mode.md)

2.4 [Variables](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/2.4-Variables)
2.4 [Variables](pages/2.4-Variables.md)

2.5 [Data types](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/2.5-Data-types)
2.5 [Data types](pages/2.5-Data-types.md)

2.6 [Type Conversions](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/2.6-Type-Conversions)
2.6 [Type conversions](pages/2.6-Type-conversions.md)

2.7 [Operators](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/2.7-Operators)
2.7 [Operators](pages/2.7-Operators.md)

2.8 [Comparisons](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/2.8-Comparisons)
2.8 [Comparisons](pages/2.8-Comparisons.md)

2.9 [Interaction: alert, prompt, confirm](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/2.9-Interaction:-alert,-prompt,-confirm)
2.9 [Interaction: alert, prompt, confirm](pages/2.9-Interaction.md)

2.10 [Conditional operators: if, '?'](https://github.com/Bunlong/The-Modern-JavaScript-Tutorial/wiki/2.10-Conditional-operators:-if,-'%3F')
2.10 [Conditional operators: if, '?'](pages/2.10-Conditional-operators.md)

2.11 Logical operators

Expand Down
131 changes: 131 additions & 0 deletions pages/1.1-An-Introduction-to-JavaScript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# 1.1 An Introduction to JavaScript

Let’s see what’s so special about JavaScript, what we can achieve with it and which other technologies play well with it.

## What is JavaScript?

JavaScript was initially created to “make webpages alive”.

The programs in this language are called scripts. They can be written right in the HTML and execute automatically as the page loads.

Scripts are provided and executed as a plain text. They don’t need a special preparation or a compilation to run.

In this aspect, JavaScript is very different from another language called [Java](https://en.wikipedia.org/wiki/Java "Java").

***
#### Why JavaScript?

When JavaScript was created, it initially had another name: “LiveScript”. But Java language was very popular at that time, so it was decided that positioning a new language as a “younger brother” of Java would help.

But as it evolved, JavaScript became a fully independent language, with its own specification called [ECMAScript](https://en.wikipedia.org/wiki/ECMAScript "ECMAScript"), and now it has no relation to Java at all.
***

At present, JavaScript can execute not only in the browser, but also on the server, or actually on any device where there exists a special program called the [JavaScript engine](https://en.wikipedia.org/wiki/JavaScript_engine "JavaScript engine").

The browser has an embedded engine, sometimes it’s also called a “JavaScript virtual machine”.

Different engines have different “codenames”, for example:

- [V8](https://en.wikipedia.org/wiki/JavaScript_engine "V8") – in Chrome and Opera.

- [SpiderMonkey](https://en.wikipedia.org/wiki/SpiderMonkey "SpiderMonkey") – in Firefox.

- ... There are other codenames like “Trident”, “Chakra” for different versions of IE, “ChakraCore” for Microsoft Edge, “Nitro” and “SquirrelFish” for Safari etc.

The terms above are good to remember, because they are used in developer articles on the internet. We’ll use them too. For instance, if “a feature X is supported by V8”, then it probably works in Chrome and Opera.

***
#### How engines work?

Engines are complicated. But the basics are easy.

1. The engine (embedded if it’s a browser) reads (“parses”) the script.
2. Then it converts (“compiles”) the script to the machine language.
3. And then the machine code runs, pretty fast.

The engine applies optimizations on every stage of the process. It even watches the compiled script as it runs, analyzes the data that flows through it and applies optimizations to the machine code based on that knowledge. At the end, scripts are quite fast.
***

## What can in-browser JavaScript do?

The modern JavaScript is a “safe” programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it.

The capabilities greatly depend on the environment that runs JavaScript. For instance, [Node.JS](https://en.wikipedia.org/wiki/Node.js "Node.JS") supports functions that allow JavaScript to read/write arbitrary files, perform network requests etc.

In-browser JavaScript can do everything related to webpage manipulation, interaction with the user and the webserver.

For instance, in-browser JavaScript is able to:

* Add new HTML to the page, change the existing content, modify styles.

* React to user actions, run on mouse clicks, pointer movements, key presses.

* Send requests over the network to remote servers, download and upload files (so-called [AJAX](https://en.wikipedia.org/wiki/Ajax_(programming) "AJAX") and [COMET](https://en.wikipedia.org/wiki/Comet_(programming) "COMET") technologies).

* Get and set cookies, ask questions to the visitor, show messages.

* Remember the data on the client-side (“local storage”).

## What CAN’T in-browser JavaScript do?

JavaScript’s abilities in the browser are limited for the sake of the user’s safety. The aim is to prevent an evil webpage from accessing private information or harming the user’s data.

The examples of such restrictions are:

* JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It has no direct access to OS system functions.

Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like “dropping” a file into a browser window or selecting it via an <input> tag.

There are ways to interact with camera/microphone and other devices, but they require a user’s explicit permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the [NSA](https://en.wikipedia.org/wiki/National_Security_Agency "NSA").

* Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other if they come from different sites (from a different domain, protocol or port).

This is called the “Same Origin Policy”. To work around that, both pages must contain a special JavaScript code that handles data exchange.

The limitation is again for user’s safety. A page from http://anysite.com which a user has opened must not be able to access another browser tab with the URL http://gmail.com and steal information from there.

* JavaScript can easily communicate over the net to the server where the current page came from. But its ability to receive data from other sites/domains is crippled. Though possible, it requires explicit agreement (expressed in HTTP headers) from the remote side. Once again, that’s safety limitations.

## What makes JavaScript unique?

There are at least three great things about JavaScript:

* Full integration with HTML/CSS.

* Simple things done simply.

* Supported by all major browsers and enabled by default.

Combined, these three things exist only in JavaScript and no other browser technology.

That’s what makes JavaScript unique. That’s why it’s the most widespread tool to create browser interfaces.

While planning to learn a new technology, it’s beneficial to check its perspectives. So let’s move on to the modern trends that include new languages and browser abilities.

## Languages “over” JavaScript

The syntax of JavaScript does not suit everyone’s needs. Different people want different features.

That’s to be expected, because projects and requirements are different for everyone.

So recently a plethora of new languages appeared, which are transpiled (converted) to JavaScript before they run in the browser.

Modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language and autoconverting it “under the hood”.

Examples of such languages:

* [CoffeeScript](http://coffeescript.org/ "CoffeeScript") is a “syntactic sugar” for JavaScript, it introduces shorter syntax, allowing to write more precise and clear code. Usually Ruby devs like it.

* [TypeScript](http://www.typescriptlang.org/ "TypeScript") is concentrated on adding “strict data typing”, to simplify development and support of complex systems. It is developed by Microsoft.

* [Dart](https://www.dartlang.org/ "Dart") is a standalone language that has its own engine that runs in non-browser environments (like mobile apps). It was initially offered by Google as a replacement for JavaScript, but as of now, browsers require it to be transpiled to JavaScript just like the ones above.

There are more. Of course even if we use one of those languages, we should also know JavaScript, to really understand what we’re doing.

## Summary

* JavaScript was initially created as a browser-only language, but now it is used in many other environments as well.

* At this moment, JavaScript has a unique position as the most widely-adopted browser language with full integration with HTML/CSS.

* There are many languages that get “transpiled” to JavaScript and provide certain features. It is recommended to take a look at them, at least briefly, after mastering JavaScript.
59 changes: 59 additions & 0 deletions pages/1.2-Code-editors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 1.2 Code editors

A code editor is the place where programmers spend most of their time.

There are two archetypes: IDE and lightweight editors. Many people feel comfortable choosing one tool of each type.

## IDE

The term [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment "IDE") (Integrated Development Environment) means a powerful editor with many features that usually operates on a “whole project”. As the name suggests, that’s not just an editor, but a full-scale “development environment”.

An IDE loads the project (can be many files), allows navigation between files, provides autocompletion based on the whole project (not just the open file), integrates with a version management system (like git), a testing environment and other “project-level” stuff.

If you haven’t considered selecting an IDE yet, look at the following variants:

* [WebStorm](http://www.jetbrains.com/webstorm/ "WebStorm") for frontend development and other editors of the same company if you need additional languages.

* [Visual Studio](https://www.visualstudio.com/ "Visual Studio") is fine if you’re a .NET developer, and a free version is available (Visual Studio Community)

* [Netbeans](http://netbeans.org/ "Netbeans").

All of the IDEs except Visual Studio are available on Windows, MacOs and Linux. Visual Studio doesn’t work on Linux.

Most IDEs are paid, but have a trial period. Their cost is usually negligible compared to a qualified developer’s salary, so just choose the best one for you.

## Lightweight editors

“Lightweight editors” are not as powerful as IDEs, but they’re fast, elegant and simple.

They are mainly used to instantly open and edit a file.

The main difference between a “lightweight editor” and an “IDE” is that an IDE works on a project-level, so it loads much more data on start, analyzes the project structure if needed and so on. A lightweight editor is much faster if we need only one file.

In practice, lightweight editors may have a lot of plugins including directory-level syntax analyzers and autocompleters, so there’s no strict border between a lightweight editor and an IDE.

The following options deserve your attention:

* [Visual Studio Code](https://code.visualstudio.com/ "Visual Studio Code") (cross-platform, free).

* [Atom](https://atom.io/ "Atom") (cross-platform, free).

* [Sublime Text](http://www.sublimetext.com/ "Sublime Text") (cross-platform, shareware).

* [Notepad++](https://notepad-plus-plus.org/ "Notepad++") (Windows, free).

* [Vim](http://www.vim.org/ "Vim") and [Emacs](https://www.gnu.org/software/emacs/ "Emacs") are also cool, if you know how to use them.

## My favorites

The personal preference of the author is to have both an IDE for projects and a lightweight editor for quick and easy file editing.

I’m using as a lightweight editor – [Sublime Text](http://www.sublimetext.com/ "Sublime Text") or [Vim](http://www.vim.org/ "Vim").

## Let’s not argue

using for a long time and are happy with.

There are other great editors in our big world. Please choose the one you like the most.

The choice of an editor, like any other tool, is individual and depends on your projects, habits, personal preferences.
51 changes: 51 additions & 0 deletions pages/1.3-Developer-console.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 1.3 Developer console

Code is prone to errors. You are quite likely to make errors… Oh, what am I talking about? You are absolutely going to make errors, at least if you’re a human, not a [robot](https://en.wikipedia.org/wiki/Bender_(Futurama) "robot").

But in the browser, a user doesn’t see the errors by default. So, if something goes wrong in the script, we won’t see what’s broken and can’t fix it.

To see errors and get a lot of other useful information about scripts, browsers have embedded “developer tools”.

Most often developers lean towards Chrome or Firefox for development, because those browsers have the best developer tools. Other browsers also provide developer tools, sometimes with special features, but are usually playing “catch-up” to Chrome or Firefox. So most people have a “favorite” browser and switch to others if a problem is browser-specific.

Developer tools are really powerful, there are many features. To start, we’ll learn how to open them, look at errors and run JavaScript commands.

## Google Chrome

Javascript error is hidden from a regular visitor’s eyes so let’s open developer tools to see it.

Press `F12` or, if you’re on Mac, then Cmd+Opt+J.

The developer tools will open on the Console tab by default.

The exact look of developer tools depends on your version of Chrome. It changes from time to time, but should be similar.

* Here we can see the red-colored error message

* On the right, there is a clickable link to the source bug.html:12 with the line number where the error has occurred.

Below the error message there is a blue > symbol. It marks a “command line” where we can type JavaScript commands and press `Enter` to run them (`Shift+Enter` to input multi-line commands).

Now we can see errors and that’s enough for the start. We’ll be back to developer tools later and cover debugging more in-depth in the chapter Debugging in Chrome.

## Firefox, Edge and others

Most other browsers use `F12` to open developer tools.

The look & feel of them is quite similar. Once you know how to use one of them (you can start with Chrome), you can easily switch to another.

## Safari

Safari (Mac browser, not supported by Windows/Linux) is a little bit special here. We need to enable the “Develop menu” first.

Open Preferences and go to “Advanced” pane. There’s a checkbox at the bottom: Show Developer menu in menu bar.

Now `Cmd+Opt+C` can toggle the console. Also note that the new top menu item named “Develop” has appeared. It has many commands and options.

## Summary

* Developer tools allow us to see errors, run commands, examine variables and much more.

* They can be opened with `F12` for most browsers under Windows. Chrome for Mac needs `Cmd+Opt+J`, Safari: `Cmd+Opt+C` (need to enable first).

Now we have the environment ready. In the next section we’ll get down to JavaScript.
Loading