0% found this document useful (0 votes)
16 views16 pages

ICT Portion g12

This document contains information about JavaScript including definitions of variables, functions, comments, alerts, confirms, and prompts. It provides code examples to declare variables, write comments, create alerts and confirms, and define basic functions. The key topics covered are using var to declare variables, assigning values to variables, writing single and multi-line comments, and using function to define functions with opening and closing curly braces.

Uploaded by

mallahtala0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views16 pages

ICT Portion g12

This document contains information about JavaScript including definitions of variables, functions, comments, alerts, confirms, and prompts. It provides code examples to declare variables, write comments, create alerts and confirms, and define basic functions. The key topics covered are using var to declare variables, assigning values to variables, writing single and multi-line comments, and using function to define functions with opening and closing curly braces.

Uploaded by

mallahtala0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Final Exam-Term 1-2023/2024

ICT Portion
Grade 12
TECHNOJAVASCRIPT
What is JavaScript?

JavaScript is a computer programming language. It is used to make web pages more interactive.
To interact with a web page by having it respond to user actions . Examples of user actions include
a click of a button, hovering over text, a mouse pointer etc.

What is an Alert Box?

The alert box is simply a box that encloses the message you need to pass to the user. The box will
pop up on the browser.

Use the alert box when you need to emphasize a particular message! It will make sure that the
user has read your message.

Creating an Alert Box


You are only required to use a function named alert and it will create the alert box for you.
alert("Hello Alex, welcome to my web page")

2
Write a program in JavaScript to Create an alert box

Output:
What is a Confirm Box?
The confirm box comes with two buttons, OK and CANCEL buttons.

The confirm box is used to let the user confirm or verify their action

Creating a Confirm Box :

Output:
What are Comments?
These are explanations for your code. You add the comments within the code to know
the meaning of each line of code.

Your friend/classmate may want to read the JavaScript code that you have written.
How can you ensure that they find it easy to understand your code? You can use
comments.
How to write Comments

To mark a line as a comment, just add two forward slashes (//) at its beginning.
Note that the comment is added within the same file as the JavaScript code, only
that it will have no effect on the results your code returns. For example:

// Hello Alex, this is a comment explaining the code to you.

You may want to give long explanations about the code. In this case, the comment
will take up more than one line. In such a case, he can enclose the comment with /*
and */. For example:

/* Hello Alex, this is a comment explaining the code to you. I need to write a program
that prints Hello World on the web browser. I hope you will enjoy seeing my code
work */

5
This code is used to print Hello world

To write the JavaScript code ,we need a text editor.


To test the JavaScript code ,we need a web browser.

6
What is a Variable?

A variable is a container in which you can store a value.


.
Creating a Variable

To create a variable in JavaScript, we use the var keyword.


A keyword is a word that is special to a programming language, that is, it is used for performing a
specific task.
To state the value that will be stored in the variable/container, that is, the variable, we use the =
operator, known as the assignment operator.
In our case, we will store Alex in the variable student, meaning that the variable will be holding the
value Alex. We can do it as follows:
var student = "Alex";
Write a program using java script code to create a variable
called student and assign value (Alex) to the variable ,Then
print the name.
What is a Variable?

A variable is simply a storage location. We use it to store values.

What is a Variable Declaration?

Variable declaration is the process where you instruct the computer to


give you some space in its memory to store something. The computer
will then allocate you some memory space that you can use to store
your variable.

How to Declare a Variable

You should use a keyword named var.


For example: var name;
The JavaScript rules that you must follow when declaring a variable:

1. The variable name should begin with either an underscore (_), a letter (either A to Zor ato
z), or the dollar sign ($). From this rule, these are valid variable declarations:
Valid Names
var _name;
var name;
var Name;
var $name;
var name12;
The following are invalid variable declarations in JavaScript:

var 23name;
var #name;
var –name;
var ()name;
2. After the first letter, one can use digits in the variable name. For example:
Valid Names

var name12;

var na23me;

var _name23;

var $name23;

3. JavaScript variable names are case sensitive, example, p is different from P.


How to assign a value to a variable?

To assign value to the variable ,you should use the = symbol .

The equal symbol is called the assignment operator.

Example: var name= “Value”;

The value of a variable could vary:

To store the value Alex into our container, we can type the following statement:

var name = "Alex";

You must have noticed that there are double quotes around the value of the variable.

It is because the value is a string.

A string is text (Word ,Sentence, Question ,List if items)


Create a variable, give it the name age, and store a value of 12 inside that container.

var age = 12;

You must have noticed that there are no double quotes around the value of the
variable.

It is because the value is a numerical value (An Integer).

Integer is a whole number.


what a function is?
It is a block of code or a set of JavaScript statements that perform related tasks.

Some JavaScript functions such as alert, confirm and prompt functions.

How to Define a Function

we use the function keyword, write the name of the function ,() then }{

Create a function by the name myfunction.

You can use the following code for this:

function myfunction(){
}

A JavaScript variable can belong to any of the following two types:


• Local variables
• Global Variables
What is a Local Variable?

A local variable is a variable that has been defined within a function, that is, within the { and } of
a function.

For example:

function myfunction(){
var name = "Alex“
}
Global Variables

A global variable is exactly the opposite of a local variable.


A local variable is defined within a function, but a global variable is defined outside a function.

For Example:

<script>

var name = "Alex" function

myfunction(){
}
</script>

You might also like