Outputting Text
In JavaScript output can be directed to several places including the current document, window and
pop-up dialog boxes.
Outputting text to the document window.
The [Link]() and [Link]() Methods
The document object in JavaScript includes two methods designed for outputting text to the client
window: write() and writeln(). In JavaScript, methods are called by combining the object name with
the method name:
[Link]-name
Data that the method needs to perform its job is provided as an argument in the parentheses; for
example:
[Link]("Test");
[Link]('Test');
Arguments are data provided to a function or a method for use in its calculations and
processing. They are provided (or passed) to the function or method by listing them in the
parentheses following the function or method name. Multiple arguments are separated by
commas.
The write() method outputs text and HTML as a string of text to the current window in sequence with
the current HTML
The writeln() method is the same as the write() method except that it adds a carriage return at the end
of the string that is being output.
End of Output /Special characters
In JavaScript, strings of text, such as those used to produce output with the write() and writeln()
methods, can include special keystrokes to represent characters that can't be typed, such as new lines,
tabs, and carriage returns.
Table showing Special characters in strings.
Character Description
\n Newline
\t Tab
\r carriage return
\f form feed
\b Backspace
For example, the following command displays the text "It Works!" followed by a newline:
[Link]("It Works!\n");
All special characters start with a backslash (\). This is called escaping characters. Escaping a character
is used in many scripting and programming languages to represent a character that cannot be typed or
that has special meaning in the language and would be interpreted incorrectly if left unescaped.
Interacting with the user-Working with dialog boxes
• Creating output in a alert dialog box using the alert() method
Syntax : [Link](string); OR alert(string);
Example: [Link] ("Click OK to continue."); or alert ("Click OK to continue.");
• Prompting users for input in a prompt dialog box using the prompt() method
Syntax: window. prompt (message, default); or prompt(message, default);
Example : [Link](“Enter your name”, “”);
[Link](“Enter your name”, “Mumia”);
• Allowing user to confirm his action before processing with execution basing on his
response in confirm dialog box.
Syntax: window. confirm(question); OR confirm(question);
Example: window. confirm(“Are sure you want to save?”);
Refer to the practical we did in class over dialog boxes.
WORKING WITH DATA AND INFORMATION
Data Types in JavaScript
Type Example
Numbers Any number, such as 17, 21.5 or 54e7
Strings "Greetings!" or 'Fun!'
Boolean Either true or false
Null A special keyword for exactly that—the null value (that is, nothing)
Literals
The term literals refers to the way in which each of the four data types are represented. Literals are fixed
values which literally provide a value in a program. For example, 11 is a literal number, "hello" is a
string literal and true is a boolean literal.
Casting
JavaScript is what is called a loosely typed programming language. In loosely typed languages, the type of
a literal or variable is not defined when a variable is created and can, at times, change based on the
context.
JavaScript has the parseInt() and parseFloat() functions, which convert strings into integers or floating
point numbers (known as casting).
For instance, parseInt("13") returns the integer 13 and parseFloat("45.2") returns the floating point
number 45.2.
It is still possible to cast a number into a string as in "Count to " + 10 evaluating to a string with the
value "Count to 10".
Creating Variables
In order to make working with data types useful, you need ways to store values for later use. This is
where variables come in.
A variable is a name assigned to a location in computer’s memory to store data. A variable allows a
programmer to create programs that are flexible and dynamic- such a program using a variable allows
us input different values each time a program is run
In JavaScript you can create variables that can contain any type of data. Variables have names, and
after assigning values to a variable, you can refer to the value by name. If you subsequently assign a
new value to the variable, you can continue referring to that new value by the name of the variable.
Declaring Variables
In order to use a variable, it is good programming style to declare it. Declaring a variable tells JavaScript
that a variable of a given name exists so that the JavaScript interpreter can understand references to
that variable name throughout the rest of the script.
Although it is possible to declare variables by simply using them, declaring variables helps to ensure
that programs are well organized and to keep track of the scope of variables
You can declare a variable using the var command:
var example;
In this line, you have defined a variable named example, which currently has no value. It is also possible
to assign value to a variable when you declare it:
var example = "An Example";
Here you have declared the variable named example and assigned a string value to it of "An Example".
Because JavaScript allows variables to also be declared on first use, the command
example = "An Example";
would also achieve the same result.
The equal sign (=) used in assigning a value to a variable is known as an assignment
operator. Assignment operators are discussed later in this chapter.
var example="An Example";
[Link](example);
The text An Example will be displayed on the document
Variables can hold string literals, numbers, or Boolean values.
var x = 10;
x = “ten”;
In the above case, the variable x is first assigned the integer value of 10 and then the string value of
the word ten. This is possible because, as we mentioned, JavaScript is loosely typed. This means that a
JavaScript variable can hold a value of any data type.
Valid identifier names
An identifier is a name you give to a variable or a function. .x and example in the above examples are examples of valid
identifiers.
[Link] property and method names in JavaScript, variable names are case sensitive.
ii. In addition, variable names must start with a letter or an underscore (_). After that, the
remaining characters can also include numbers.
iii. An identifier cannot contain space.
iv. An identifier cannot match any of the reserved words
v. A constant is a variable whose value doesn’t change.
Note: You can also declare multiple variable names with the same var keyword