Introduction to Javascript
Chapter 14 · Tamil Nadu State Board
- 7 min read
- 6-question quiz
Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content
In short
This lesson introduces JavaScript as a language for making interactive web pages and checking user input before it is sent to a server. It explains how to add JavaScript to HTML, write basic programs, and work with variables, data types, expressions, and operators. It also teaches how comments and popup boxes are used in JavaScript. These ideas help students understand how web pages can respond to users and perform useful tasks.
Written by AI from this lesson. How we check it
Key points
- JavaScript can make web pages interactive and dynamic.
From your textbook
“JavaScript programming language is used.”
- Client-side validation can check information before sending it to the server, reducing server traffic.
From your textbook
“User entered data in the Dynamic Web page can be validated before sending it to the server.”
- JavaScript is placed in HTML using script tags, usually within the head section.
From your textbook
“it is normally recommended that should be kept it within the <head> tags.”
- JavaScript variables are declared with var and can hold strings, numbers, or Boolean values.
From your textbook
“The basic data types in JavaScript are Strings, Numbers, and Booleans.”
- A global variable can be used throughout the JavaScript code, while a local variable is limited to its function.
From your textbook
“Variables declared within a function are defined only within the body of the function.”
- JavaScript operators perform calculations, comparisons, logical operations, and string concatenation.
From your textbook
“Operators are used for JavaScript’s arithmetic expressions, comparison expressions, logical expressions, assignment expressions.”
- Alert, confirm, and prompt boxes provide warnings, ask for consent, or collect user input.
From your textbook
“JavaScript supports three kind of popup boxes: Alert box, Confirm box, and Prompt box.”
Formulas in this chapter
- JavaScript script segment<script language="javascript" type="text/javascript"> JavaScript code </script>Explained here
- Writing a string and variabledocument write ("string " + var);Explained here
Introduction to JavaScript
JavaScript is a scripting language used to create and test Internet applications. It helps turn static HTML pages into interactive, dynamic web pages. JavaScript can also check information entered in a form before sending it to a Web Server.
Advantages of JavaScript
- It helps create interactive, dynamic web pages.
- It can validate user-entered data before that data is sent to the server.
- Validating data first can reduce server traffic and server load.
- It supports interface items such as textboxes, buttons, drag-and-drop components and sliders.
Remember: JavaScript can check form entries in the browser before they are sent to the Web Server.
Adding JavaScript to an HTML Page
Place JavaScript code between <script> and </script> tags. The browser treats the text between them as script commands. The tags can be placed anywhere in the web page, but keeping them within the <head> tags is normally recommended.
<script language="javascript" type="text/javascript"> JavaScript code </script>
language specifies the scripting language, and type indicates the scripting language with the value text/javascript.
Worked example
What does this first JavaScript program display?
Try it first, then show the solutionSolution
- The code calls document.write with the text "Hello World!".
Answer: Hello World!
Steps to run a JavaScript program
- Enter the HTML and JavaScript code using a text editor.
- Save the latest version of the code.
- Open the file in a browser to see the result.
- If the document is already loaded, refresh it or press F5 to reload it.
JavaScript Lexical Structure
Lexical structure means the basic rules for writing a program. These rules cover names, comments and how statements are separated. JavaScript is case-sensitive, and it ignores spaces between tokens.
JavaScript comments
| Comment style | How it is written |
|---|---|
| Single-line comment | Write // before the comment text. JavaScript ignores the text to the end of that line. |
| Multiline comment | Write the comment text between /* and */. JavaScript ignores the text inside. |
Other basic rules
- Semicolons (;) are used to separate statements and can mark their ends.
- A literal is a data value written directly in a program.
- An identifier is a name used for variables, functions or certain loop labels.
- Reserved words cannot be used as identifiers.
JavaScript Variables and Data Types
Definition
- Variable
- A variable is a named memory location where a value can be stored.
Variables are declared with the var keyword. Declaring a variable tells the interpreter that a new identifier is being used and allocates storage space for data. Examples are var no; and var no1,no2;.
Basic JavaScript data types
| Data type | Meaning |
|---|---|
| String | A list of characters written inside single or double quotes. It can include spaces and special characters such as \n. |
| Number | An integer or floating-point numerical value. |
| Boolean | One of two values: true or false. |
Rules for naming variables
- The first character must be a letter or an underscore (_), not a number.
- The rest of the name can contain letters, numbers and underscores. It cannot contain spaces or other symbols and punctuation marks.
- Names are case-sensitive. RegisterNumber and registernumber are different variable names.
- There is no limit to the length of a variable name.
- Reserved words cannot be used as variable names.
Remember: A variable name must begin with a letter or underscore, and it must not be a reserved word.
Variable Scope and Values
Global and local variables
| Global variable | Local variable |
|---|---|
| Can be defined throughout JavaScript code. | Declared within a function and available only inside that function’s body. |
A variable’s scope is the part of the source code where it is defined and can be used. You can assign a value when declaring a variable, such as var numericData1 = 522;. One var statement can declare several variables separated by commas, such as var no1=50, no2=5065;.
Values that can be assigned to variables
- Integer: var int_const=250;
- Floating point: var float_const=250.85;
- Character: var char_const=’A’;
- String: var string_const=”Raman”;
- Boolean: var boolean_const=true;
JavaScript does not require a data type to be stated when declaring a variable. A variable can first hold one data type and later hold a different data type. Variables can also be declared implicitly by using them on the left-hand side of an assignment.
Definition
- Literal
- A literal is a fixed value written in source code, often to give a variable its initial value.
document write ("string " + var);
The string is joined with the value of the variable.
Type Casting, Operators and Expressions
Definition
- Type conversion
- Type conversion, also called casting, changes a value from one data type to another.
Types of casting
- Implicit casting
- Explicit casting
Implicit casting happens automatically when the data stored in a variable is changed. An operator combines values, called operands, to produce a new value. An expression is one or more variables or constants joined by operators, and evaluating it produces a result.
Expression types
- Arithmetic expressions
- Relational expressions
- Logical expressions
Remember: An expression is evaluated to produce a value, which may be an integer, floating-point value or Boolean value.
Arithmetic and Assignment Operators
Arithmetic operators
| Operator | Meaning and example |
|---|---|
| + | Addition: 20 + 120 gives 140. |
| – | Subtraction: 20 – 120 gives 100. |
| * | Multiplication: 10 * 100 gives 1000. |
| / | Division: 100/522 gives 5.22. |
| % | Modulus gives the remainder: 100 % 522 gives 22. |
Definition
- Assignment operator
- An assignment operator gives a new value to a variable; in JavaScript, = is the assignment operator.
You can assign a literal or an expression to a variable. You can also chain assignments, as in var m = n = z = 25;. This sets all three variables to 25.
Shorthand arithmetic operators
| Operator | Meaning |
|---|---|
| += | Add and assign |
| -= | Subtract and assign |
| *= | Multiply and assign |
| /= | Divide and assign |
| %= | Find the remainder and assign |
A shorthand operator performs an arithmetic operation and stores the result in the same variable. For example, if var sum = 120;, then sum += 20; makes sum equal to 140.
Relational or Comparison Operators
Definition
- Relational operator
- A relational operator compares two values and gives a result of true or false.
Comparison operators
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
A comparison expression is true when its condition holds and false when it does not. For x=10 and y=20, x<y is true, while x>y is false.
Remember: Relational operators produce a Boolean result: true or false.
Questions students ask
Short, direct answers to what students search for in this chapter.
What is JavaScript used for?
JavaScript is a scripting language used to create Internet applications and interactive, dynamic web pages. It can also check information entered in a form before that information is sent to a Web Server.
How do you add JavaScript to an HTML page?
Write JavaScript code between <script> and </script> tags. The browser interprets the text between these tags as script commands. The tags can be placed anywhere in the page, though keeping them within the <head> tags is normally recommended.
What are the basic data types in JavaScript?
The basic data types are Strings, Numbers and Booleans. A string is a list of characters, a number is an integer or floating-point value, and a Boolean is either true or false.
What are the rules for naming JavaScript variables?
A variable name must start with a letter or underscore, not a number. The rest can contain letters, numbers and underscores, but not spaces or other symbols. Names are case-sensitive, can have any length, and cannot be reserved words.
What is the difference between a global variable and a local variable in JavaScript?
A global variable has global scope and can be defined throughout JavaScript code. A local variable is declared within a function and is available only in that function’s body.
What are JavaScript literals?
A JavaScript literal is a fixed value written in source code, often to initialize a variable. Values can be integer, floating point, character, string or Boolean. Examples include var int_const=250; and var boolean_const=true;.
Show 13 more questions
What is type casting in JavaScript?
Type casting, also called type conversion, changes a value from one data type to another. JavaScript has implicit casting and explicit casting. Implicit casting happens automatically when the data stored in a variable is changed.
What are shorthand arithmetic operators in JavaScript?
Shorthand arithmetic operators perform a calculation and assign its result to the same variable. JavaScript supports +=, -=, *=, /= and %=. For example, sum += 20; adds 20 to sum and stores the result in sum.
What do relational operators return in JavaScript?
Relational operators compare two values and return true if the stated condition holds, or false if it does not. The operators include ==, !=, <, >, <= and >=. For example, if x=10 and y=20, x<y is true.
What do &&, || and ! mean in JavaScript?
&& (AND) returns true when both expressions are true. || (OR) returns true when either expression, or both expressions, are true. ! (NOT) returns true when its expression is false; otherwise, it returns false.
How does JavaScript use + to join strings?
The + operator adds numbers and also joins strings. If any operand is a string, + is interpreted as string concatenation. For example, joining "Java" and "Script" gives "JavaScript".
What is the difference between ++m and m++?
Both forms increment m by 1. With ++m, the increment happens before the expression returns its value. With m++, the expression returns m's original value first, and then m is incremented.
What does the typeof operator return in JavaScript?
typeof returns a string that identifies the operand's data type. The listed results are "number", "Boolean", "string", "function" and "undefined". Its operand may be a literal, variable, function or object.
How does the JavaScript conditional operator work?
The conditional operator ?: tests a condition and chooses between two values. If the condition is true, the value before the colon is assigned. If the condition is false, the value after the colon is assigned.
What are the three JavaScript popup boxes?
The three JavaScript popup boxes are alert, confirm, and prompt. Alert displays a warning message. Confirm asks the user to choose OK or Cancel. Prompt displays a text box for user input and also provides OK and Cancel buttons.
What does a JavaScript alert box do?
An alert box mainly displays a warning message to the user. It has one button, labelled OK. For example, it can warn the user when a required input field has been left empty. It can be called using alert() or window.alert().
What does confirm() return when the user clicks Cancel?
When the user clicks Cancel in a confirm dialog box, confirm() returns false. If the user clicks OK, it returns true. The box is used to get the user's consent about an option.
What does prompt() return when the user clicks Cancel?
When the user clicks Cancel in a prompt dialog box, prompt() returns null. If the user clicks OK, it returns the value entered in the text box. A prompt takes a displayed label and a default string.
How do you write comments in JavaScript?
A single-line JavaScript comment begins with //, and the interpreter ignores the rest of that line. A multiple-line comment begins with /* and ends with */. Comments add remarks to source code and are ignored by the JavaScript interpreter.
Book back answers
28 textbook exercises, solved and checked against the book. Questions we could not prove are left out.
Part-I Choose the correct answer
Which provided a common scripting language to web developers to design, test and deploy Internet Application
- A) C
- B) C++
- C) Java
- D) JavaScript
Try it, then show the answerAnswer
D) JavaScript
JavaScript provides a common scripting language for web developers to design, test and deploy Internet applications.
From the book: “It provides a common scripting language for Web developers to design, test and deploy Internet Applications.”
Expand CGI
- A) Common Gateway Interface
- B) Complex Gateway Information
- C) Common Gateway Information
- D) Complex Gateway Interface
Try it, then show the answerAnswer
A) Common Gateway Interface
The chapter expands CGI as Common Gateway Interface.
From the book: “Common Gateway Interface (CGI) programs on the Server”
JavaScript programming language is used to develop the
- A) Dynamic Web Page
- B) Window
- C) Web Page
- D) Home Page
Try it, then show the answerAnswer
A) Dynamic Web Page
JavaScript is used to develop interactive, dynamic web pages.
From the book: “to develop such interactive pages (Dynamic Web page ) JavaScript programming language is used.”
The Dynamic Web Page help to save server’s
- A) Work
- B) Route
- C) Traffic
- D) Pvath
Try it, then show the answerAnswer
C) Traffic
Validating data on a dynamic web page before sending it to the server saves server traffic.
From the book: “This saves server traffic, which means less load on your server.”
Practice
Why can checking form data in a Dynamic Web page be useful?
Exam questions
Practice questions with model answers. These are not from an official paper.
How does JavaScript client-side validation help before form data reaches the server?2 marks
It checks the entered data in the web page before submission. Validating data before sending it reduces server traffic and the load on the server.
What are the basic JavaScript data types? Briefly describe them.2 marks
The basic types are Strings, Numbers, and Booleans. Strings contain characters, Numbers can be integers or floating-point values, and Booleans are true or false.
Explain the difference between global and local variables.2 marks
A global variable has global scope and can be defined throughout the JavaScript code. A variable declared within a function is local and is defined only inside that function's body.
Describe the three JavaScript popup boxes and what each is used for.5 marks
An alert box gives a warning and has an OK button. A confirm box asks for consent and provides OK and Cancel, returning true or false. A prompt box asks the user for input and returns the entered value if OK is selected, or null if Cancel is selected.
Watch out
- Do not confuse the prompt box with the confirm box: prompt collects text input, while confirm asks for consent.
- Do not assume variable names that differ only in letter case refer to the same variable.
Still stuck on this lesson?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →