[Go to site: main page, start]

0% found this document useful (0 votes)
8 views29 pages

JavaScript Basics: Comments, Variables, and Functions

The document provides an overview of JavaScript, including its comments, keywords, identifiers, variables, constants, input/output management, type conversion, operators, data types, control structures (if statements, switch-case, loops), functions, and events. It outlines the syntax and usage of various elements in JavaScript programming. Additionally, it explains how to embed JavaScript code in web pages.

Uploaded by

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

JavaScript Basics: Comments, Variables, and Functions

The document provides an overview of JavaScript, including its comments, keywords, identifiers, variables, constants, input/output management, type conversion, operators, data types, control structures (if statements, switch-case, loops), functions, and events. It outlines the syntax and usage of various elements in JavaScript programming. Additionally, it explains how to embed JavaScript code in web pages.

Uploaded by

abhishek7feb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JavaScript

It is the scripting language of the Web.

Type of Comments
a) Single Line Comments
A single line comments start with // and it will be ignored by
JavaScript.

b) Multi-line Comments
A multi-line comments start with /* and end with */ and it will be
ignored by JavaScript.
Keywords and Identifiers
Keywords
All the JavaScript keywords are predefined word that cannot be used as
identifiers.

Identifiers
In JavaScript, identifiers are used to name different elements.

Note:

In JavaScript, the first character must be a letter, or an underscore (_), or a


dollar sign ($).
Variables and Constants
Variables
JavaScript variables are the names assigned to the memory blocks in the computer’s
memory where we store our values.

Example
var x = 5;
var y = 10;
var z = x + y;

Note
Here var is a keyword and x,y,z are variables.

Constants
JavaScript constants are the value stored into the memory blocks in the computer’s
memory.

Note
Here 5, 10 are constants.
Q. How to manage Input and Output?
1) Input
The prompt() method displays a dialog box that prompts the user
for input.
Syntax: result = [Link] (“message”,
“default”);
2) Output
It can "display" data in different ways:

1. The [Link]() method writes a string of


text.
Syntax: [Link](“textString”);
2. The [Link]() method displays an alert box.

Syntax: [Link](“textString”);
Q. How to do type Conversion -
These can be used to convert strings to numbers:

Method Description

parseInt( ) It parses a string and returns an integer

parseFloat( ) It parses a string and returns a floating


point number
List of Operators –
The diff types –
1) Unary Operators
These type of operator needs only one
operand to perform any operation.
Ex. &, sizeof( ), ++, -- etc.
2) Binary Operators
These type of operator needs two
operands to perform any operation.
3) Ternary Operators
These type of operator needs three
operands to perform any operation.
Ex. ? : etc

1. Assignment Operators
= x=5

+= x += 5 x=x+5

-= x -= 5 x=x–5

*= x *= 5 x=x*5
/= x /= 5 x=x/5

%= x %= 5 x=x%5
2. Comparison Operators
Operato Description
r

== equal to

!= not equal

> greater than

< less than

>= greater than or equal to

<= less than or equal to


3. Logical Operators
Operato Description
r

&& AND

|| OR

! NOT
4. Bitwise Operators
Operator Description

& AND

| OR

~ NOT

^ XOR

<< Left shift

>> Right shift;


How to embed JS code?
There are 2 ways to embed code in a webpage:

[Link] an internal script


Ex. <html>
<body>
<script>
alert(“Welcome to JScript”);
</script>
</body>
</html>

[Link] an external script


Ex. <html>
<body>
<script rel=”text/javascript” src=”[Link]”>
</script>
</body>
</html>
Now open Notepad and type the following code as below:
alert(“Welcome to JScript”);
Just save it as [Link] and refresh the webpage.

3.
Data Types -
There are two types of data types in JavaScript.

1. Primitive
2. Non-primitive (reference)
1. Primitive data type
The predefined data type are called primitive data types.
Ex -

a) number Ex. 5, 3.5 etc

b) string Ex. “Harry Potter” etc

c) boolean Ex. True or False etc

d) undefined

e) NULL
2. Non-primitive data type
The user defined data type are called non-primitive data
types.
Ex -
a) Array

b) RegEx
The if statements
1. if
2. if-else
1. if

It will execute the if block if it’s CONDITION evaluates to


true.
Syntax:
if(CONDITION){
statement(s);
}
2. if else

It will execute the if block if it’s CONDITION evaluates to true


otherwise
it will execute the else block.
Syntax:
if(CONDITION){
statement(s);
}
else{
statement(s);
}
The Switch-Case statements -
Syntax:
switch(var){
case val1: statement(s);
break;
case val2: statement(s);
break;

default: statement(s);

}
The Loops in JavaScript
In JS loops are used to iterate or repeat any task again and again.

There are mainly two types of loops:


1. Entry Controlled loops:
2. Exit Controlled loops:

1. Entry Controlled loops:


In this type of loops the test condition is checked at the entry of the
loop.
Ex. while and for loops.

2. Exit Controlled Loops:


In this type of loops the test condition is checked at the end of loop.
Ex. do-while loop.
1. while Loop

It iterates the statement if testExpression evaluates to true.

Syntax:
initilize;
while(testExp){
statement(s);
increment;
}
Ex:
i=0;
while(i<5){
statement(s);
i++;
}
2. for Loop

It iterates the statement if


testExpression evaluates to true.

Syntax:
for(init; testExp; incre){
statement(s);
}
Ex:
for(i=0; i<5; i++){
statement(s);
}
3. do-while Loop

It iterates the statement if testExpression evaluates to true.


Note: Here… it is guaranteed to run the loop at least once even if the condition
becomes false for the first time.

Syntax:
initilize;
do{
statement(s);
increment;
}while(testExp);
Ex:
i=0;
do{
statement(s);
i++;
}while(i<5);
The Functions in JavaScript
A function is a reusable block of code to perform any specific task.

Q. How to define a function?

Syntax:

function func(arg1, arg2, …){


exps;
statement(s);
}

func(para1, para2, …)
Q. How to call a function?

Syntax:
func_name(val1, val2, …);

Example-
function greet(){
alert(“Hi…”);
}

greet();
greet();
greet();
The JavaScript Events
HTML events are "things" that happen to HTML elements.
Events Descriptions

onclick It fires when user clicks.


ondblclick It fires when user double clicks.
oncontextmenu It fires when user right clicks.
onfocus It fires when a form element gains focus.
onblur It fires when a form element lose focus.
onkeypress It fires when user press then release any key.
onkeydown It fires when user press any key.
onkeyup It fires when user release any key.
onmousemove It fires when mouse moves.
onmousedown It fires when the left mouse button is pressed.
onmouseup It fires when the left mouse button is released.
onmouseover It fires mouse get in an element.
onmouseout It fires mouse get out an element.
onload It fires when a webpage is launched.
onunload It fires when a webpage is closed.

You might also like