Home » Online Computer Education » Learn Working with Values in Java Script, Lesson 12

Learn Working with Values in Java Script, Lesson 12

 

Working with Values
JavaScripts and JScripts store information as values. For example, if you were to create a form for your users to fill out, each entry in the form would contain a separate value. Your scripts can store these values in variables and then use the variables throughout your script.

The list of the types of values supported by JavaScript and JScript.

JAVASCRIPT AND JSCRIPT VALUES

Value                                   Description

Boolean                                  A value that indicates a condition of either true or false

Null                                         An empty value

Numbers                               A numeric value such as 99 or 3.142

Strings                                   A string of text such as “Welcome” .
Declaring Variables in Java Script.

To use a variable in your script, that variable must first be declared. You have two ways of doing this. The first option is to use the var keyword as demonstrated in the following example:

var firstName = “Alexander”;
This example creates a variable named firstName and assigns it the value of Alexander. Your scripts would probably contain other variables that also contain information such as last names, phone numbers, and so on.

Optionally, you can also declare a variable by simply referencing it for the first time as shown in the following example:

firstName = “Alexander”;
USE THE <NOSCRIPT> TAG TO TALK TO NON-JAVASCRIPT BROWSERS

Although more than 95 percent of the browsers being used today support JavaScript, there are still many that do not. In addition, both the Netscape and Internet Explorer browsers enable users to turn off JavaScript support. What can you do to make your JavaScript-enhanced pages available to those visitors who want to view them while still making basic information available to visitors who do not have JavaScript-enabled browsers?

One option you can explore is to create both JavaScript and non-JavaScript versions of your HTML pages and display the appropriate set of pages based on an inspection of the user’s browser. I will talk more about this option later today.

A simpler solution is to display an HTML page that provides two links with an instruction to your visitors to click on one link if their browser supports JavaScript and to click on the other if it does not. However, you may be taking a big risk by assuming that all your visitors even know what JavaScript is and that their browsers support it.

A really simple alternative is to use the <NOSCRIPT> tags. Every browser, even those with JavaScript support disabled, will recognize these HTML tags. Their purpose is to display a message for browsers that do not process your JavaScript. JavaScript-enabled browsers will ignore everything within the <NOSCRIPT> tags.

The following example demonstrates how to set up a page that can provide information to browsers, regardless of their level of JavaScript support.

<HTML>
<HEAD>
<TITLE>Script 2.2 – The NOSCRIPT tag</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!–Start hiding JavaScript statements
document.write(“Non-JavaScript browsers will not see this
message ” +
“but JavaScript-enabled browsers will.”);
// End hiding JavaScript statements –>
</SCRIPT>
<NOSCRIPT>
JavaScript-enabled browsers will not see this message but
JavaScript handicapped browsers will see it.
</NOSCRIPT>
</BODY>
</HTML>

In this example, a value of Alexander is assigned to a variable named firstName (if the variable exists), thereby changing its value. If the referenced variable does not yet exist, it is created and assigned a value.

Whether you choose to use the var keyword or not when creating variables is really just a matter of personal preference. The main benefit of using var is that it makes your scripts easier to read and also lets you declare variables for later use in your scripts without assigning them an initial starting value.

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *