Skip to content

Latest commit

 

History

History
71 lines (40 loc) · 4.16 KB

read04.md

File metadata and controls

71 lines (40 loc) · 4.16 KB

JavaScript

JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions. While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as Node.js, Apache CouchDB and Adobe Acrobat. JavaScript is a prototype-based, multi-paradigm, single-threaded, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles. Read more about JavaScript.

This section is dedicated to the JavaScript language itself, and not the parts that are specific to Web pages or other host environments. For information about API specifics to Web pages, please see Web APIs and DOM.

The standards for JavaScript are the ECMAScript Language Specification (ECMA-262) and the ECMAScript Internationalization API specification (ECMA-402). The JavaScript documentation throughout MDN is based on the latest draft versions of ECMA-262 and ECMA-402. And in cases where some proposals for new ECMAScript features have already been implemented in browsers, documentation and examples in MDN articles may use some of those new features.

Do not confuse JavaScript with the Java programming language. Both "Java" and "JavaScript" are trademarks or registered trademarks of Oracle in the U.S. and other countries. However, the two programming languages have very different syntax, semantic, and use.

Editor or IDE Any text editor can be used.

On MS Windows you can even use the built-in plain Notepad, but I'd recommend something more powerful. You can download Notepad++ which is very similar to Notepad, but with tons of extra features, or get Aptana Studio. The latter has a steeper learning curve, so you might want to start with the more simple tool.

Embed or include You can either embed the JavaScript code directly inside the HTML file, or you can put a line in the HTML file that will include the external JavaScript file. In most cases the latter is recommended, but for our first examples, in order to make the whole thing work in a single file, we'll embed the JavaScript code inside some HTML.

In order to do that we add the <script> opening and </script> closing tags. Between the two we write our JavaScript code.

Input Output The very first thing we need to learn is how to interact with the JavaScript code running in the browse. There are a number of way JavaScript can display text for the user (output). The most simple one is by using the alert function:

alert This will show a pop-up in the browser with the text. (You can click on Try! that will open the specific script in a separate window.) The alert() function is actually rarely used, but it is an easy way to show the use of JavaScript.

examples/js/alert.html

<script language="javascript"> alert("Hello World"); </script>

Try! If you'd like to try it yourself, open your editor and create a file with .html extension (for example hello.html) and put the above code in the file. Then switch to your browser and open the file with the browser. (Most browsers will let you do that using the File/Open File menu option.)

document.write examples/js/document_write.html

First line

<script> document.write("

Hello World

"); </script>

Last line

Try! In this example we have some text (First line), then the JavaScript code, and then some more text (Last line). The JavaScript code uses the document.write function to change the content of the page. It will embed the html snippet

Hello World

after the "First line", but before the "Last line".

This function was often used when one wanted to change what's shown. Today, there are some more advanced techniques.

console.log Finally let's see how developers usually print out debugging information.

examples/js/console.html

<script> console.log("Hello World"); </script>

Try! Most of the web browsers provide what is called a "JavaScript console". It is an additional window which is normally not visible, where the browser can print out warnings and errors generated by the execution of the JavaScript code. (E.g. if there is a syntax error in the code.) The developer can also print information to this console using the console.log() call.

@Bara Awadallah