Data types
Javascript data types
Javascript is a loosely typed language, meaning you do not have to declare the data types of variables explicitly. Javascript uses a variant datatype and performs conversions automatically as necessary.
JavaScript has six types of data, usually referred to as primitive data types. The main types are strings, numbers, objects, and Booleans. The other two are null and undefined .
Ternary if
Ternary if statement
This provides a shorthand way to assign different values to a variable, depending on a condition.
var myVariable = document.getElementById("color").value
myVariable == "red" ? 1 : 0;
This is identical to:
Switch ... case
Javascript switch .. case statement
The following function performs the same test using the switch case statement. As you can see it is much more compact and consequently easier to read.
Nested if statements
Nested if ... else statements
|
Very often several conditional tests may have to be performed, resulting in IF ... ELSE statements being nested one inside another. This can quickly get messy, and its very easy to miss-match a curly brace. Flow charts are a useful paper and pencil technique for sorting out more complex algorithms. |
Javascript template
About the example templates
While the W3C and ECMAscript (Javascript) standards continue to evolve, browser compliance with any particular version, at any given time, is a moving target. All vendors have added their own tweaks (additional features?) to the standards, which by their very definition, are non standard; so should be avoided if the goal is cross browser compatibility.
The examples therefore focus on standard features, ignoring any cross browser differences, except where specifically mentioned.
Object model
The JavaScript object
This page lists the built in objects, and collections which together comprise the JavaScript object. Some browsers may provide additional (non standard) properties or methods that do not form part of the EMCA scripting specification, or the W3C standards, that specify the components of HTML,XHTML,XML, DOM and CSS.
Functions
Javascript Functions
Javascript functions are the basic building blocks which can be combined together to provide more complex functions. Each of the short programs you have written so far, have started with the word function. The process of structured analysis (or top down analysis) is to take a complex problem and break it down into more manageable chunks. These chunks become the basic building blocks of your program. They let you combine to several operations (program statements) to complete some recognisable task.Variables
Defining variables
Variables are used in Javascript to store values in your scripts. Choosing a suitable name for a variable can help in understanding what a script does. Although not strictly necessary, it is good practice to declare variables before using them. You do this using the var statement.
Naming Variables
Javascript is case-sensitive so naming a variable myVar is different to the variable myvar.Programming preface
A preface to programming
Before looking at any specific programming language, there is some jargon we need deal with first. Today there are many different programming languages, here are just a few, So what's different about them?
Javascript, Python, Java, C++, PHP, PROLOG
- 2 comments
- Read more
- 362 reads
SmartSim - counters
SmartSim - Synchronous counters
In this example we'll use SmartSim to build a Synchronous counter using JK flip flops. No not a type of shoe, but a digital circuit that has memory, able store 1 bit of information. It works in much the same way as light switch in your home. When you turn a light ON, the switch remembers to keep the light on, until you decide to turn it OFF. I.e. it has two states, ON and OFF, or in binary logic terms TRUE and FALSE.

