This article constitutes section 2, part A, chapter 2 of the CIW Website design manager course briefly covers: a JavaScript Variables and Data.

Communicating with the User

I am not sure why a chapter entitled variables and data should start with a topic on interactive methods, but it does. There are three methods for communicating with the user: alert, confirm, prompt and write.

The alert() method displays a pop-up dialog with a text message. alert(“Hello World!”);
The confirm() method also displays a pop-up dialog with a text message, but it now includes the OK and Cancel buttons and will return a true or false value depending on the button clicked.

The prompt()  method goes a stage further and allows the input of text.

You may click on the method names to see sample output. If you view this page’s source you will also be able to study the embedded JavaScript that facilitates this.

JavaScript Variables and Data

The write() method allows text to be written to the browser window or document as it is referred to within the JavaScript object model. Because the write() method belongs to the document object it should be referenced thus: document.write()

Variables

JavaScript variables can store four data types but these are not explicitly declared. Variables should be declared, but the data type is not specified.

Variable names in JavaScript are case-sensitive and it is good practice to follow the established naming convention. A variable name, and a function name for that matter, should use two words without an intervening space, with the second word capitalised. For example: firstName or bigNumber. The first character should be a letter or an underscore, subsequent characters may be letters, numbers or the underscore.

JavaScript contains Keywords, the names of built-in functions, methods and commands, which may not be used as variable or function names.

JavaScript Keyword Examples:

  • break
  • continue
  • do
  • else
  • false
  • for
  • function
  • if
  • in
  • int
  • new
  • null
  • return
  • switch
  • true
  • typeof
  • var
  • void
  • while
  • with

JavaScript variables also contain Reserved words that, while not currently in use may be used in the future, so they cannot be used as variable or function names.

JavaScript Reserved Word Examples:

  • abstract
  • byte
  • catch
  • default
  • delete
  • extends
  • final
  • goto
  • import
  • interface
  • native
  • package
  • protected
  • short
  • static
  • super
  • throw
  • transient
  • try

Expressions for JavaScript Variables

There are four types of expression in JavaScript. These are Assignment, Arithmetic, String and Logical. An assignment operator assigns a value to a variable, an arithmetic operator perform an arithmetic operation which evaluates to a number and may be assigned to a variable, a string operator evaluates to a string and a logical operator evaluates to a true/false value.

OperatorTypeDescription
=AssignmentAssigns the value of the right operand to the left operand.
+=AssignmentAdds together the operands and assigns the result to the left operand.
-=AssignmentSubtracts the right operand from the left operand and assigns the result to the left operand.
*=AssignmentMultiplies the operands and assigns the result to the left operand.
/=AssignmentDivides the left operand by the right operand and assigns the result to the left operand.
%=AssignmentDivides the left operand by the right operand and assigns the remainder to the left operand.
+ArithmeticAdds together the operands.
-ArithmeticSubtracts the right operand from the left operand.
*ArithmeticMultiplies the operands.
/ArithmeticDivides the left operand by the right operand.
%ArithmeticDivides the left operand by the right operand and calculates the remainder.
+StringCombines the operands into a single string.
++UnaryIncreases the value of the supplied operand by one.
-UnaryNegates the value of the operand.
--UnaryDecreases the value of the supplied operand by one.
&&LogicalEvaluates to true when both operands are true.
||LogicalEvaluates to true when either operand is true.
!LogicalEvaluates to true if the operand is false, and to false if the operand is true.
double equalsComparisonEvaluates to true if the operands are equal.
!=ComparisonEvaluates to true if the operands are not equal.
>ComparisonEvaluates to true if the left operand is greater than the right operand.
<ComparisonEvaluates to true if the left operand is less than the right operand
>=ComparisonEvaluates to true if the left operand is greater than or equal to the right operand.
<=ComparisonEvaluates to true if the left operand is less than or equal to th right operand.

A good understanding of these operators, their variables and data. and their use in basic expressions will provide the foundation for the bulk of your JavaScript programming.

External Links

Tutorials Point

Why not take a peek at our most recently curated post?

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close