JavaScript Tutorial
7. Miscellaneous
[this page | pdf | back links]
We set out below some
further comments on JavaScript
that may help developers.
JavaScript syntax:
-
Statements are separated by semicolons, e.g. var x, y, z; x = 3;
-
The language consists of values, operators, expressions, keywords and
comments
-
Values can be literals (fixed values) or variables.
-
Number literals are written with or without decimal points, e.g. 100 or 10.1
(commas should not be used as the decimal separator)
-
String literals are text, written within double or single quotes, e.g. x = "35"; y = 'a';
-
Variables are declared using the var keyword,
e.g. var x; var y = 10;
-
The equal sign is used to assign a value to a variable, e.g. x = 3;, i.e. it is the assignment
operator
-
An expression is a combination of values, variables and operators
-
String concatenation is achieved by +,
e.g. "a" + " " + "b" evaluates to "a b"
-
Comments are noted by text after double slashes “//” or between /*
and */
-
Identifiers are used to name variables. The first character must be a
letter, an underscore character (“_”)
or a dollar sign (“$”)
-
JavaScript is case sensitive, e.g. firstName,
FirstName and firstname will be three different
variables
-
JavaScript typically uses ‘lower camel case’ when joining multiple words
together, i.e. first letter of first word is lower case, first letter of each
subsequent word is upper case, e.g. “firstName”.
Some methods and properties specifically use this convention, and because JavaScript
is case sensitive it means that using a different convention will result in an
incorrect (or failed) evaluation. Developers often adopt the same convention
when naming variables.
-
JavaScript uses the Unicode character set
Displaying (text / numerical) data using JavaScript:
-
Text and values can be inserted into an HTML element, using e.g. the innerHTML property
-
We can write to the browser output stream, using e.g. document.write() (this is useful for
testing purposes)
-
We can write to an alert box, using e.g. window.alert()
-
We can write into the browser console, using e.g. console.log()
Location of any JavaScript within HTML webpages:
-
HTML
in-file JavaScript needs to be inserted in script elements.
-
Any number of scripts can be included in an HTML document and can be
placed in either the <body>
or the <head>
section
Global methods and properties:
JavaScript has some global properties and functions
that can be used with all built-in JavaScript objects. These include:
Global properties:
Property
|
Description
|
More
|
Infinity
|
A number that is
positive infinity (-Infinity
refers to a number that is negative infinity)
|
Here
|
NaN
|
Not-a-Number result of
a numeric calculation
|
Here
|
undefined
|
Indicates variable has
not been assigned a value
|
Here
|
Global methods:
These are perhaps best
referred to as global ‘functions’ since they are called globally.
Method / Function
|
Description
|
More
|
Boolean()
|
Converts an object’s
value to a Boolean (i.e. true
or false)
|
Here
|
decodeURI()
|
Decodes URI
|
Here
|
decodeURIComponent()
|
Decodes URI component
|
Here
|
encodeURI()
|
Encodes URI
|
Here
|
encodeURIComponent()
|
Encodes URI component
|
Here
|
escape()
|
Depreciated. Use encodeURI() or encodeURIComponent() instead
|
Here
|
eval()
|
Evaluates a string as
if it were script code
|
Here
|
isFinite()
|
Returns true if finite, otherwise false
|
Here
|
isNaN()
|
Returns true if NaN, otherwise false
|
Here
|
Number()
|
Converts an object’s
value to a number
|
Here
|
parseFloat()
|
Parses a string and
returns a floating-point number
|
Here
|
parseInt()
|
Parses a string and
returns an integer
|
Here
|
String()
|
Converts an object’s
value to a string
|
Here
|
unescape()
|
Depreciated. Use decodeURI() or decodeURIComponent() instead
|
Here
|
Properties shared by multiple JavaScript objects:
JavaScript includes some
properties that can be applied to any object and can allow the user to alter
the methods and properties applicable to the object. These include:
Property
|
Description
|
More
|
constructor
|
Returns object’s
constructor function
|
Here
|
length
|
Returns the length of
the object
|
Here
|
prototype
|
Allows author to add
properties and methods to an object
|
Here
|
Conversions between variable types:
An area of JavaScript
that can be confusing (and hence can lead to errors) is the handling of
conversions between different variable types. JavaScript is ‘weakly typed’,
i.e. variables can change type in some circumstances. Examples of what happens
when variables are explicitly converted using global type conversion functions
are set out below. It is important to bear in mind that type conversion can
also happen implicitly, e.g. if we try to concatenate a number with a string
then the number will be converted to a string beforehand.
Original value (x)
|
Result of conversion to
a string, using String(x)
|
Result of conversion to
a number, using Number(x)
|
Result of conversion to
a Boolean, using Boolean(x)
|
false
|
"false"
|
0
|
false
|
true
|
"true"
|
1
|
True
|
0
|
"0"
|
0
|
false
|
1
|
"1"
|
1
|
True
|
"0"
|
"0"
|
0
|
false
|
"1"
|
"1"
|
1
|
True
|
NaN
|
"NaN"
|
NaN
|
false
|
Infinity
|
"Infinity"
|
Infinity
|
True
|
-Infinity
|
"-Infinity"
|
-Infinity
|
True
|
""
|
""
|
0
|
false
|
"10"
|
"10"
|
10
|
True
|
"ten"
|
"ten"
|
NaN
|
True
|
[ ]
|
""
|
0
|
True
|
[10]
|
"10"
|
10
|
True
|
[5,10]
|
"5,10"
|
NaN
|
True
|
["ten"]
|
"ten"
|
NaN
|
True
|
["five","ten"]
|
"five","ten"
|
NaN
|
True
|
function(){}
|
"function(){}"
|
NaN
|
True
|
{ }
|
"[object Object]"
|
NaN
|
True
|
null
|
"null"
|
0
|
False
|
undefined
|
"undefined"
|
NaN
|
False
|
It is worth noting that
some of the global type conversion functions are mimicked by type conversion
functions attached to specific object types. The behaviours of the two
apparently similar functions are not always identical, as the ones attached to
specific object types will include a conversion into the relevant type before
there is an attempt to convert the variable into its eventual output type.
Recent developments:
In recent years, the capabilities and therefore
sophistication of the JavaScript
language has grown considerably. Browser developers have put a considerable
amount of effort into arranging for JavaScript code to run as quickly as
possible within browsers. For example, they have developed ‘just in time’
compilation techniques to supplant older purely interpreted ways of executing
the JavaScript statements. JavaScript as a language has been standardised and
object-orientated programming tools such as error trapping
have been added. Event handling (which is particularly important for browser
based programs) has been further refined. Its DOM has
continued to evolve and become more sophisticated as website usage has expanded
and evolved.
NAVIGATION LINKS
Contents | Prev | Next | JavaScript