JavaScript in a nutshell – keywords. Basics part 1.

JavaScript file

Level: basic

Welcome to the first part of articles about JavaScript language basics. At the beginning I want to describe things useful for beginners, to start programming as soon as possible. More advanced programmers simply may use it as some kind of handbook, having information in one place.

We’ll start from JavaScript language keywords. The history of JavaScript is interesting, but I won’t write about this topic here, as I want to focus on more practical things.

The list below contains reserved words in JavaScript. Many of them (marked with a star) are reserved for future compatibility. In short words: many of them will be officially supported in future versions of the language. Probably we don’t have to wait too long. And these words are well-known by Java programmers for sure.

Here is the full list:

abstract (*)as (*)boolean
breakbytecase
catchcharclass (*)
continueconst (*)debugger (*)
defaultdeletedo
doubleelseenum (*)
export (*)extends (*)false
finalfinallyfloat
forfunctiongoto (*)
ifimplements (*)import (*)
ininstanceofint
interface (*)is (*)long
namespace (*)native (*)new
nullpackage (*)private (*)
protected (*)public (*)return
shortstatic (*)super (*)
switchsynchronized (*)this
throwthrows (*)transient (*)
truetrytypeof
use (*)varvoid
volatile (*)whilewith

The matter of embedding scripts in page source

A few words about how to embed scripts.

According to XHTML:

<script type="text/javascript">
//<![CDATA[
  document.write("Hello!");
//]]>
</script>

According to HTML:

<script language="JavaScript">
 <!--
  document.write("Hello!");
 -->
</script>

Please pay attention to the comments after opening and closing script tags.

On the other hand it, including code from external .js file we do in following way:

// local file
<script type="text/javascript" src="myscript.js"></script>

// file from external source
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

OK, I think it should be mentioned about JavaScript objects now. The language is based on objects, although in many places is different compared to other object-oriented languages​​.

Main objects in JavaScript

– location – Inside this object information about the location of current page is stored. Full address and particular elements (domain, port, protocol and others).

– history – Here we find addresses of all sites visited during the current session. The object also provides methods to change current page (navigation through the history).

– document – In this object we will find a collection of information concerning current website (the document). Information about all elements, such as images, links, forms and their elements, appearance of elements, and so on. We can say: all about the current document.

– form – This object contains information about forms placed on the current page, and gives us an access to objects of the form (fields, lists, button, etc.). Also provides methods to handle these elements.

– navigator – Refers to the browser.

– window – It’s an object representing the entire window. Objects window and document are intensively used in JavaScript codes.


Properties and methods (and few words about functions)

Two types of resources are available in objects.

Properties – These are the variables, that describes the object we’re working on. We refer to them according to following schema:

object.property

Example – the document object contains title property (the title of the document):

alert(document.title);

 

Methods – These are programming commands, kind of functions BUT directly associated with a specific object.

Whereas the “normal” functions are not associated with object. This is the fundamental difference between them.

Example – the document object owns a method to write text (also HTML code) on the page:

document.write("Yes!");

Another popular example is call of the go() method, to navigate one page back:

<a href="javascript:history.go(-1)">Back</a>

 

Functions

As I mentioned, methods are related to the object, and the usual functions are independently.

Following simple example illustrate it. We call the function and we don’t need any object here:

function simpleFunc(a, b) {
  if (a > b)
    return a;
  else 
    return b;
}

// call and display the result using alert()
alert(simpleFunc(5, 10));

Summary

Here we finish the first part of introduction to programming in JavaScript. In the next part we tell more about functions and events.

Thank you.

Both comments and pings are currently closed.

One Response to “JavaScript in a nutshell – keywords. Basics part 1.”

  1. […] mentioned in basics of JavaScript, there is a reserved keyword class – for future […]