Child pages
  • JavaScript Basics

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Stoc

Introduction

...

To JavaScript

JavaScript was originally developed by Brendan Eichof Netscape under the name Mocha, which was later renamed to LiveScript and finally to JavaScript. JavaScript code, much like other programming languages, is made up of statements which serve to make assignments, compare values and execute other sections of code.

...

  • Is interpreted line-by-line
  • Is case sensitive
  • Ignores whitespaces
  • Uses semicolon at the end of each statement
  • Uses blocks (with curly braces)
  • Uses identing in code
  • Uses commenting symbols
  • Uses keywords

Basic

...

Programming Terminology

JavaScript has some basic programming terms:

...

Note
titleNote

For a complete list of reserved words, please check the TODO: reference to reserved words section.

Execution

...

Order

JavaScript is interpreted (executed) line-by-line as it is found in a piece of code (script)

Case

...

Sensitivity

JavaScript is case sensitive, meaning that capital letters are distinct from their lowercase counterparts.

...

  • Keywords
  • operators
  • Variable names
  • Event handlers
  • Object properties

Lowercase

...

Keywords

All JavaScript keywords are lowercase. For example, when you use a feature like an if statement, make sure that you type if and not If or IF.

Camel

...

Casing (camel-back)

JavaScript uses the camel casing (mixed casing) naming convention for functions, methods and properties. If a name is one word, then the name is made of all lowercase characters. If more words are used to form a name, the first word is lowercase followed by the next word(s) which all start with a capital letter.

...

JavaScript applies these rules to the use of the whitespace in coding.

Whitespace

...

Ignored

JavaScript ignores those characters that take up space on the screen without visual representation or without necessary meaning.

...

Code Block
x=x+1

or:

Code Block
x = x + 1

Whitespace

...

Not Ignored (exception)

However, most operations other than simple arithmetic functions require a space to make their meaning clear.

...

Code Block
var s = 'This spacing is      preserved.';

Using

...

Semicolons

A semicolon indicates the end of a JavaScript statement. You can use a semicolon for:

  • A single statement
  • Multiple statements on one line
  • Multiple statements on multiple lines

Single

...

Statement

Example:

Code Block
x = x + 1;

Multiple

...

Statements On One Line

Example:

Code Block
x = x + 1; y = y + 1; z = 0;

Multiple

...

Statements On Multiple Lines

Example:

Code Block
x = x + 1;
y = y - 1;
Note
titleNote

You do not need to use a semicolon for the last statement of a method (script).

Using

...

Curly Braces

JavaScript uses curly braces {} to group a list of statements together into one larger statement, known as a block statement.

...

Code Block
var count = 10; // holds a number of items

Multiple

...

Line Comments

Multiple line comments (similar to C programming) are enclosed between forward slash and asterix / at the beginning of the comment and an asterix and forward slash / at the end of the comment. Everything in between is ignored by the interpreter.

...

Example:

Code Block
var x = 2;

JavaScript

...

Grammar

Here are the main elements of JavaScript grammar:

...

Note
titleNote

You should not use variables without first declaring them. Using a variable on the right-hand side of an assignment without first declaring it will result in an error.

Introduction

...

To Declaring Variables

When you declare a variable you can do that with or without assigning a value to it.

...

Code Block
var x, y = 1, z;

Data

...

Types

JavaScript knows two categories of data types:

  • Primitive (basic) data types
  • Composite data types

Primitive

...

Data Types

These are primitive or basic data types, which contain one kind of data, for variables:

...

Note
titleNote

When undefined and NULL data types are being compared, the result will be true.

Composite

...

Data Types

Composite data types are made up of strings, numbers, booleans, undefined values, null values and even other composite types. These are the three composite types:

...

Functions are used to keep code that performs a particular job in one place, making the code reusable. In Servoy, built-in functions are accessible from the method editor.

Weak

...

Typing

One of the major differences between JavaScript and other programming languages is that JavaScript is weakly typed. This means that the type of variable is inferred from the variable's content. As a result, the data type can change the moment you put data into the variable of another type than it previously contained.

...

Operator

Example

Description

==

a == b

Returns true if a and b are equal.

!=

a != b

Returns true if a and b are not equal.

>

a > b

Returns true if a is greater than b.

<

a < b

Returns true if a is less than b.

>=

a >= b

Returns true if a is greater than or equal to b.

<=

a <= b

Returns true if a is less than or equal to b.

Tertiary

...

Statement

Here is a tertiary statement:

...

Code Block
myString.length

Functions

...

And Methods

A JavaScript function is quite similar to a procedure or subroutine in other programming languages. A function is a discrete set of statements which perfom some actions. It may accept incoming values (parameters) and it may return an outgoing value.

...

The Servoy Solutions Object Model (SOM) and the W3C Document Object Model (DOM) are both object models.

Object

...

Model

An object model:

  • Defines the interface used by scripts to look at and manipulate structured information, like an HTML document.
  • Determines the make-up and characteristics of an object's component parts.
  • Defines how the parts work.

...

Here an overview of the Servoy SOM:

Object

...

References

Here is a list of the JavaScript object references and how they apply to Servoy:

...