JavaScript - Part One
If “Software is Eating the World”, then JavaScript has the biggest appetite. I believe JavaScript will become an increasingly important programming language, especially in the advent of Node.js, which is an open-source, cross-platform JavaScript runtime environment that can be executed server-side.
As a result, I thought it was about time I brushed up on my JavaScript skills. This article is the first in a series that will capture my key notes for JavaScript. These articles are not intended to be a tutorial, instead please look at them as a set of “cheat sheets”.
What is JavaScript?
JavaScript is a high-level, dynamic, untyped, and interpreted programming language, meaning implementations execute instructions directly, without the need to compile a program into machine-language instructions.
JavaScript has been standardized in the ECMAScript (ES) language specification, which is currently on version 5, with version 6 (ES6 or ECMAScript 2015) set to be ratified in 2015.
JavaScript is primarily used for client-side scripting on the World Wide Web, but is increasingly being positioned for writing server-side applications and services using Node.js.
Ground Rules
In JavaScript, almost everything is an object.
- Booleans can be objects (if defined with the new keyword).
- Numbers can be objects (if defined with the new keyword).
- Strings can be objects (if defined with the new keyword).
- Dates are always objects.
- Maths are always objects.
- Regular expressions are always objects.
- Arrays are always objects.
- Functions are always objects.
- Objects are always objects.
All JavaScript values, except primitives, are objects.
This may not immediately make sense, but it is very important. Remember, in JavaScript, objects are king!
Operands
An operand is a term used to describe any object that is capable of being manipulated. For example, in 1 + 2
the “1” and “2” are the operands and the “+” symbol is the operator.
Operators
JavaScript has arithmetic, string, and logical operators. There are both binary and unary operators. A binary operator requires two operands, one before the operator and one after the operator. For example, 3 + 4
or x * y
, where the “+” symbol is the operator.
The w3schools includes a useful reference of JavaScript Operators.
Statements
A statement performs an action. For example, “loops” and “if statements” are examples of statements. Wherever JavaScript expects a statement, you can also write an expression (see below). However, the reverse does not hold true, for example you cannot write a statement where JavaScript expects an expression.
Expressions
An expression produces a value and can be written wherever a value is expected. The value may be a number, string or a logical value. Conceptually, there are two types of expressions: those that assign a value to a variable (e.g. x = 10
), and those that simply have a value (e.g. 4 + 6
). In both examples, the expression evaluates to “10”.
Properties
JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name and a value. Properties can usually be changed, added, and deleted, but some are read only.
The syntax for accessing the property of an object is objectName.property
.
A property’s value can be a function, in which case the property is known as a method.
Primitives
Primitives are values that have no properties. In JavaScript there are five types of primitive:
- undefined
- null
- boolean
- string
- number
Everything else is an object (remember, objects are king). The primitive types boolean, string and number can be wrapped by their object counterparts.
Keywords
In JavaScript, some identifiers are reserved words and cannot be used as variables or function names. For example:
- true
- false
- this
- typeof
- return
I want to draw particular attention to the this
keyword. In JavaScript, this
, is the object that “owns” the JavaScript code. For example:
- The value of
this
, when used in a function, is the object that “owns” the function. - The value of
this
, when used in an object, is the object itself. - The
this
keyword in an object constructor does not have a value. It is only a substitute for the new object. - The value of
this
will become the new object when the constructor is used to create an object.
The this
keyword can be very useful and therefore is worth understanding. However, remember that this
is a keyword, not a variable. Therefore, you cannot change the value of this
.
For a full list of keywords please refer to JavaScript Reserved Words.
Regular Expressions
A regular expression is a sequence of characters that forms a search pattern.
In JavaScript, regular expressions are often used with the two string methods: search() and replace().
The structure of a “regular expression” can be found below:
/pattern/modifiers;
An example of a simple search using a regular expression can be found below:
var y = "Visit LifeinTECH";
var z = y.search(/LifeinTECH/i);
In this example, /LifeinTECH/
is the pattern and i
is the modifier. The result would be “6”.
For a full list of JavaScript regular expressions please refer to JavaScript RegExp Reference.
Variables
Variables are named containers. You can store data within a variable and then refer to the data simply by calling the variable. Before you use a variable in a JavaScript program, you must declare it, which is achieved with the var
keyword.
An example of a variable declaration, where the variable has no value (technically it has the value of “undefined”), can be found below:
var a;
A few examples of a variable declarations with assigned values can be found below:
var a = 1;
var b = 2;
var c = "John Smith";
var d = 1 + 1;
var e = a + b;
The next part of the JavaScript Series can be found here.