Hello JavaScript

JavaScript
Now it’s time to start learning JavaScript. In this chapter, we’re going to introduce the language, as well as set up a programming environment. We’ll also write our first programs in JavaScript.

■ programming
■ the history of JavaScript
■ the tools that are needed to program in JavaScript 
■ Hello, World!―your first JavaScript program 
■ JavaScript in the console
■ JavaScript in the web browser
■ another more complicated JavaScript program

Programming

Programming is about making computers do what you want. A computer program is basically a series of instructions that tell your computer how to perform a task. Unfortunately, though, computers don’t speak the same language as us―they only use 1s and 0s. The first computers were programmed using punched cards, with a hole representing a 1 and no hole representing 0. Machine code and assembly language are low-level programming languages that are closely associated with a computer’s hardware. These can be difficult in which to program because they involve writing very abstract code that is heavily tied to a computer’s architecture.

Alternatively,high-level programming language sallow abstractions such as functions and logical statements to be used, making code easier for humans to read and write. Programs are written in a language such as C, C++ or Java, which is then compiled into machine code and executed. These programs are usually very fast and are often used to write games and professional business software where speed is important.

Interpreted high-level languages that are translated into machine code at run time are often referred to as scripting languages. They typically run slower than compiled languages, although interpreters are becoming more and more sophisticated, increasingly blurring the line between compiled and interpreted languages. 

JavaScript

The language we’ll be learning in this book is JavaScript, often referred to as the language of the Web. Nearly all web browsers can run JavaScript, making it one of the most popular programming languages in the world. It has a low barrier to entry―all you need to program in JavaScript is a text editor and a web browser. Although it is easy to get started, JavaScript can be a tricky language to grasp as it has some unique features and interesting quirks. Once you have mastered it, though, you’ll find it is a very flexible and expressive language that can create some powerful applications.

JavaScript is a high-level language that is compiled at run time. This means that it requires an engine that is responsible for interpreting programs and running them. The most common JavaScript engines are found in browsers such as Firefox, Chrome, or Internet Explorer, although JavaScript can be run without a browser. Many modern JavaScript engines use a Just-in-time (JIT) interpreting process, which considerably speeds up the compilation process, making the programs run faster.
JavaScript is also a dynamic language, which means that elements of a program can change while it is running. 

The History of JavaScript

The World Wide Web was originally a bunch of pages linked together by hyperlinks. Soon people wanted more interaction and so Netscape (an early browser vendor) asked Brendan Eich to develop a new language for their Navigator browser. This needed to be done quickly because of the intense competition between Netscape and Microsoft to be first to market, and Eich managed to create a prototype language in just ten days. In order to do this, he borrowed various elements from other languages, including AWK, Java, Perl, Scheme, HyperTalk, and Self. The new language was originally called LiveScript, but was hastily rebranded as JavaScript so that it could benefit from the publicity that the Sun Microsystem’s Java language was attracting at the time. This name has often caused some unfortunate confusion, with JavaScript often thought of as a lighter version of Java; the two languages are unrelated, although JavaScript does share some syntax with Java.

JavaScript made its debut in version 2 of Netscape’s Navigator browser in 1995. The following year, Microsoft reverse-engineered JavaScript to create their own version, called JScript to avoid copyright issues with Sun Microsystems who owned the Java trademark and had licensed it to Netscape. JScript shipped with version 3 of the Internet Explorer browser and was almost identical to JavaScript―it even included all the same bugs and quirks―but did have some extra Internet Explorer-only features. Microsoft included another scripting language called VBScript with Internet Explorer at the same time, although this never really caught on.

JavaScript (and JScript) was immediately popular. It had a low barrier to entry and was relatively easy to learn, which meant an explosion in its usage making web pages dynamic and more interactive. Unfortunately, its low barrier was also a curse as it meant that people could write snippets of code without much understanding of what they were actually doing. Code could be easily copied and pasted and was often used incorrectly, leading to lots of poor code examples appearing all over the Web. JavaScript was also frequently used to create annoying pop-up adverts and for browser sniffing (the process of detecting which browser was being used to view a web page), and it started to gain a negative reputation.

Netscape and Sun Microsystems decided to standardize the language along with the help of the European Computer Manufacturers Association, who would host the standard. This standardized language was called ECMAScript, again, to avoid infringing on Sun’s Java trademark. This caused even more confusion,but eventually ECMAScript was used to refer to the specification, and JavaScript was (and still is) used to refer to the language itself.

The ECMA Script standard can be difficult to interpret in places, so the implementations of JavaScript can vary in assorted JavaScript engines. This is why some web browsers behave differently when running JavaScript programs. 

The Browser Wars 

By the time Netscape Navigator 4 and Internet Explorer 4 were released, JavaScript had become incredibly popular. Microsoft had started a lot of hype about the term Dynamic HTML, or DHTML for short, to refer to the use of JavaScript to make HTML more interactive and dynamic. In an attempt to seize upon this popularity, Netscape and Microsoft tried to add new proprietary features, which lead to different syntaxes being used. This "arms race" of adding new features to compete became known as the Browser Wars. The unfortunate downside was that programmers had to write two versions of code to achieve the same results in each browser. Professional programmers often dismissed JavaScript as a toy language unsuitable for any serious programming, but this was unfair criticism―the language wasn’t the problem, it was the way it was being implemented and used.

Eventually, Microsoft won the browser wars and Internet Explorer emerged as the dominant browser. Support for standards had also increased, helped largely by the efforts of the Web Standards Project (WaSP). Developer and browser vendors started to work together and embrace the standards laid out by the World Wide Web Consortium (W3C) and ECMA.

The open source web browser Firefox debuted in 2002 and Apple launched the Safari browser in 2003. These both had strong standards support, which meant that developers were able to produce better web applications using JavaScript that behaved consistently across different browsers.

Web 2.0

In 2005, sites such as Google Maps, Flickr, and Gmail started to appear and successfully demonstrated that JavaScript was capable of creating rich internet applications that looked and behaved like native desktop applications. At around the same time, the term Ajax, short for Asynchronous JavaScript And XML, was coined by Jesse James Garrett. This described a technique of obtaining data from a server in the background and updating only the relevant parts of the web page without the need for a full page reload, enabling the user to still interact with the rest of the page. This created a more seamless experience for users where it was used extensively in many Web 2.0 applications. As a result a lot of professional programmers took more notice of JavaScript and it began to be seen as a powerful and flexible programming language, capable of producing high-quality code. 

Standards

As JavaScript became used for more sophisticated applications and browsers embraced standards, the JavaScript landscape changed. A new browser war started, but this time it was about seeing which browser could be the most standards-compliant.There has also been competition to increase the speed of the JavaScript engine that is built into the different browsers. This started in 2008 when engineers at Google developed the V8 engine to run inside the Chrome browser. It was significantly faster than previous JavaScript engines and signalled another arms race as other browser vendors responded by increasing the speed of their engines. JavaScript now runs significantly faster in modern browsers and the pace of improvement shows no sign of slowing down. 

HTML5

HTML5 is the latest HTML specification, although it is actually more of an umbrella term for all the latest technologies that are used on the Web. This includes HTML, CSS3 modules, and lots of APIs that use JavaScript to interact with web pages. These will be covered in more detail in Chapter 10. HTML5 has proven to be very popular and is emerging as a dominant standard for web development. JavaScript is a key feature in how some of its more interesting aspects work.  

Node.js

In 2009, Ryan Dahl developed Node.js, which allows server-side applications to be written in JavaScript. It is based on the Google's V8 engine and implements nonblocking input and outputs in an event-driven environment. This allows the creation of fast and powerful real-time web applications written exclusively in JavaScript. It has also lead to many applications and JavaScript libraries that don’t use the browser at all. Node JS has proven to be exceptionally popular and its usage continues to grow. This has increased the interest in and use of JavaScript as it starts to appear in many environments.

The popularity of Node,js has lead to an interesting development known as
Isomorphic JavaScript. This involves having the same JavaScript code that can be run
either on the client- or server-side: if a browser is unable to run the code, it can be
run on the server and downloaded, or if the server is unavailable, the code can be
run on the client.

The Future of JavaScript

These are exciting times for JavaScript as it is being used for more and more applications beyond simply making web pages interactive. A big growth area at the moment is Single Page Applications. These applications run in the browser and rely heavily on JavaScript. HTML5 games that use JavaScript extensively are also becoming increasingly popular, especially as the graphical ability of browsers continues to improve. JavaScript and HTML5 technologies can be used to develop browser extensions, Windows 8 desktop widgets, and Firefox OS and Chrome OS applications. Many non web-related applications also use JavaScript as their scripting language. It can be used to add interactivity to PDF documents, create HTML templates (Mustache), interact with a database (MongoDB), and even control robots (Cylon.js)! It certainly seems like JavaScript has a bright future. As the web platform continues to evolve and mature and its usage grows beyond the browser, JavaScript is sure to remain a central part of future developments.

0 Response to "Hello JavaScript"

Post a Comment