Site icon Tutorial

Language constructs (Variables, etc.)

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:

Values that JavaScript recognizes and describes the fundamental building blocks of JavaScript expressions: variables, constants, and literals. JavaScript recognizes the following types of values:

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:

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:

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).

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.

Back to Tutorial

Exit mobile version