JavaScript is a client-side scripting language. This means the web surfer’s browser will be running the script. There are many uses for the powerful JavaScript language. Here are a few things:
- Clocks
- Drop Down Menus
- Alert Messages
- Popup Windows
- HTML Form Data Validation
Values that JavaScript recognizes and describes the fundamental building blocks of JavaScript expressions: variables, constants, and literals. JavaScript recognizes the following types of values:
- Numbers, such as 42 or 3.14159
- Logical (Boolean) values, either true or false
- Strings, such as “Howdy!”
- null, a special keyword denoting a null value; null is also a primitive value.
- undefined, a top-level property whose value is undefined; undefined is also a primitive value.
Variables – They are used as symbolic names for values in application. The names of variables, called identifiers, conform to certain rules. A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters “A” through “Z” (uppercase) and the characters “a” through “z” (lowercase). Some examples of legal names are Number_hits, temp99, and _name.
Declaring variables – It is done in two ways:
- With the keyword var. For example, var x = 42. This syntax can be used to declare both local and global variables.
- By simply assigning it a value. For example, x = 42. This always declares a global variable and generates a strict JavaScript warning. You shouldn’t use this variant.
Evaluating variables – A variable declared using the var statement with no initial value specified has the value undefined. An attempt to access an undeclared variable will result in a ReferenceError exception being thrown:
var a;
console.log(“The value of a is ” + a); // prints “The value of a is undefined”
console.log(“The value of b is ” + b); // throws ReferenceError exception
Variable scope – Depending upon where a variable is used, it can be
Local – It is a variable declared within a JavaScript function becomes LOCAL and can only be accessed within that function. (the variable has local scope). Local variables with the same name in different functions can be present, because local variables are only recognized by the function in which they are declared.
Global – It is declaring a variable outside of any function, because it is available to any other code in the current document.
Constants – We can create a read-only, named constant with the const keyword. The syntax of a constant identifier is the same as for a variable identifier: it must start with a letter or underscore and can contain alphabetic, numeric, or underscore characters.
const prefix = ‘212’;
A constant cannot change value through assignment or be re-declared while the script is running. The scope rules for constants are the same as those for variables, except that the const keyword is always required, even for global constants. If the keyword is omitted, the identifier is assumed to represent a variable.
Literals – It represent values in JavaScript which are fixed values and various types of literals are:
Array literals – An array literal is a list of zero or more expressions, each of which represents an array element, enclosed in square brackets ([]). When you create an array using an array literal, it is initialized with the specified values as its elements, and its length is set to the number of arguments specified. The following example creates coffees array with three elements and a length of three:
var coffees = [“French Roast”, “Colombian”, “Kona”];
Boolean literals – The Boolean type has two literal values: true and false.
Floating-point literals – A floating-point literal can have the following parts:
- A decimal integer which can be signed (preceded by “+” or “-“),
- A decimal point (“.”),
- A fraction (another decimal number),
- An exponent.
The exponent part is an “e” or “E” followed by an integer, which can be signed (preceded by “+” or “-“). A floating-point literal must have at least one digit and either a decimal point or “e” (or “E”). Some examples are 3.1415, -3.1E12, .1e12, and 2E-12.
Integers – They can be expressed in decimal (base 10), hexadecimal (base 16), and octal (base 8).
- Decimal integer literal consists of a sequence of digits without a leading 0 (zero).
- Leading 0 (zero) on an integer literal indicates it is in octal. Octal integers can include only the digits 0-7.
- Leading 0x (or 0X) indicates hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F.
Object literals – An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). You should not use an object literal at the beginning of a statement. This will lead to an error or not behave as you expect, because the { will be interpreted as the beginning of a block. It is a comma separated list of name value pairs
wrapped in curly braces. It is declared or defined as:
var myObject = {}
An object literal with a few properties example:
var myExampler = {
path: ‘images/’,
speed: 4500,
images: [“smile.gif”, “grim.gif”, “frown.gif”, “bomb.gif”]
}
Functions – A function is a block of code that executes only when you tell it to execute. It can be when an event occurs, like when a user clicks a button, or from a call within your script, or from a call within another function. Functions can be placed both in the <head> and in the <body> section of a web page.
Defining a function – The syntax for function definition is
function functionname()
{
some code
}
The { and the } defines the start and end of the function and some code refers to code to be executed when called.
Function calling and arguments – When a function is called, some values can be passed to it, which are called arguments or parameters. These arguments can be used inside the function. Multiple arguments can be sent if, separated by commas (,) as
myFunction(argument1,argument2)
Declare the argument, as variables, when you declare the function:
function myFunction(var1,var2)
{
some code
}
The variables and the arguments must be in the expected order. The first variable is given the value of the first passed argument etc.