JavaScript – What is JavaScript – JavaScript Origins

About JavaScript

JavaScript Origins – It is a programming language. JavaScript was released by Netscape and Sun icrosystems in 1995. However, JavaScript is not the same thing as Java.

What is JavaScript

  • It is a programming language.
  • It is an interpreted language.
  • JS is object-based programming.
  • It is widely used and supported
  • It is accessible to the beginner.

Uses of JavaScript

  • Use it to add multimedia elements: With JavaScript you can show, hide, change, resize images, and create image rollovers. You can create scrolling text across the status bar.
  • Create pages dynamically: Based on the user’s choices, the date, or other external data, JavaScript can produce pages that are customized to the user.
  • Interact with the user: It can do some processing of forms and can validate user input when the user submits the form.

Writing JavaScript

JavaScript code is typically embedded in the HTML, to be interpreted and run by the client’s browser. Here are some tips to remember when writing JavaScript commands.

  • JS code is case sensitive
  • White space between words and tabs are ignored
  • Line breaks are ignored except within a statement
  • JS statements end with a semi- colon;

The SCRIPT Tag

The SCRIPT tag alerts a browser that JavaScript code follows. It is typically embedded in the HTML.

<SCRIPT language = "JavaScript">
  statements
</SCRIPT>

Implementing JavaScript

There are three ways to add JS commands to your Web Pages.

  • Embedding code
  • Inline code
  • External file

External File

You can use the SRC attribute of the <code>SCRIPT</code> tag to call JS code from an external text file. This is useful if you have a lot of code or you want to run it from several pages, because any number of pages can call the same external JS file. The text file itself contains no HTML tags. It is call by the following tag:

<SCRIPT SRC="filename.js"></SCRIPT>

Programming Basics

Programmers use variables to store values. A variable can hold several types of data. In JS you don’t have to declare a variable’s data type before using it. Any variable can hold any JS data type, including:

  • String data
  • Numbers
  • Boolean values (T/F)

Variable Names

There are rules and conventions in naming variables in any programming language. It is good practice to use descriptive names for variables. The following are the JS rules:

  • The variable name must start with a letter or an underscore.firstName or _myName.
  • You can use numbers in a variable name, but not as the first character. name01 or tuition$
  • You can’t use space to separate characters.userName not user Name
  • Capitalize the first letter of every word except the first salesTax or userFirstName.

Variables

  • To declare variables, use the keyword var and the variable name: var userName
  • To assign values to variables, add an equal sign and the value: var userName = “Smith”  var price = 100

Functions

With functions, you can give a name to a whole block of code, allowing you to reference it from anywhere in your program. It has built-in functions for several predefined operations. Here are three some functions.

  • alert(“message”)
  • confirm(“message”)
  • prompt(“message”)

Example:

<script type="text/javascript">
	var userName;
	userName = prompt("Enter your name", "");
	alert("Thank you, " + userName);
</script>

User-Defined Functions

With user-defined functions, you can name a block of code and call it when you need it. You define a function in the HEAD section of a web page. It is defined with the function keyword, followed by the function name and any arguments.

function functionName(argument)
{
  statements
}
Spread the love