Javascript: fourth lesson


In the previous lessons, I explained the main principles about Javascript. We talked about objects, properties, methods and events. However Javascript is a programming language, and now we have to observe closely the Javascript language's structure.

Variables and Literals

In the third lesson we talked about variables as they were some 'black box' which contain something. However there are some rules to handle variables:

  1. The variable's name must start with a letter or underscore ("_"); in other words 'mickeymouse' is a valid name, '_box123' is a valid name, but '123_var' is not a valid name

  2. Javascript is case sensitive, and so, 'MICkeyMouse' is different from 'miCKEymoUSE'

  3. you can put anything inside of a variable (well, I mean values, such as '1735' or 'aaabbbccc' or '$.$dc)99' and so on...)

  4. In order to assign a value to a variable, you have to use the '=' symbol. For example, if you want to use the variable named 'black_box', and you want to assign the '88' value to it, just type: black_box = 88

Literals unlike variables, are fixed values. Literals may be: integers, booleans and strings. Integers may be decimals, hexadecimals or octals (e.g., 36, 0xfff, -45 and so on). Boleans may be: 'true' or 'false'. Strings are zero or more characters enclosed in double (") or single (') quotes (e.g., "sun", "2home$", '/%44ssbbccddaa.23-4' and so on). A little interesting thing: Escaping Characters. Escaping characters are characters which may be enclosed inside of strings. These characters begin with a "\". The "\n" escaping character for example, means new line. So, if you use a string like this:

"This string is composed by: \n 2 lines..."

The result will be:

This string is composed by:

2 lines...

Here a list of escaping characters:

  • \b (it means 'backspace')
  • \f (it means 'form feed')
  • \n (it means 'new line')
  • \r (it means 'carriage return')
  • \t (it means 'tab')
  • \\ (it means 'backslash character')
  • \" (it means 'double quotes character')

For example, if you want to use this string: ' "this is a string" ', you have to write so: "\"this is a string\"".

Operators

There are several operators: assignment, comparison, arithmetic, bitwise, logical, string and special operators. You can use operators between 2 operands, or you can just use one operand and one operator (or vice versa one operator and one operand). For example, "=" is the assignment operator to assign a value to a certain variable:

var1 = "hello!"

var1 is the first operand, "=" is the operator and "hello!" is the second operand. There are a lot of operators, and I am not going to show you all of them. Just I'm going to explain the commonest of them.

Assignment operators

  • = (it means assign to; e.g., a = "hello")

Comparison operators

  • = = it means equal to; for example, you can use this operator inside of an if statement to compare 2 operands. It returns true if the 2 operands are equal. Example:

    a = 123

    b = 123

    so, the if a == b statement return true...

  • != it means not equal to
  • > it means greater than
  • >= it means greater or equal than
  • < it means less than
  • <= it means less or equal than

Arithmetic operators

  • ++ it means add 1 to
  • -- it means subtract 1 from

Logical operators

  • AND (or &&)
  • OR (or ||)
  • NOT (or !)

These operators are especially used inside of conditional expressions. We will observe them closely later...(if these things are too much complicated, don't worry, you will able to understand them later, when we will see some examples of scripts).


Index           Home  Back       About  Contact us!

Copyright (c) 1998-2006 Wowarea