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

JS Part 2pdf

The document provides an overview of JavaScript functions, highlighting their importance for code reusability and organization. It explains function definitions, arguments, return values, local and global variables, as well as special values like null and undefined. Additionally, it covers string manipulation methods and examples of their usage in JavaScript.

Uploaded by

Shylesh MR
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)
10 views16 pages

JS Part 2pdf

The document provides an overview of JavaScript functions, highlighting their importance for code reusability and organization. It explains function definitions, arguments, return values, local and global variables, as well as special values like null and undefined. Additionally, it covers string manipulation methods and examples of their usage in JavaScript.

Uploaded by

Shylesh MR
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

JAVASCRIPT

Part 2
Functions Advantage
Quite often we need to perform a similar action in many places of the script. Code reusability

For example, we need to show a nice-looking message when a visitor logs in, logs out and Less coding

maybe somewhere else.


Functions are the main “building blocks” of the program. They allow the code to be called
many times without repetition.
Function Example
1.<script> function name(para)
2.function msg(){ {
...body...
3.alert("hello! this is message"); }
4.}
5.</script>

The function keyword goes first, then goes the name of the function,
then a list of parameters between the parentheses
(comma-separated, empty in the example above) and finally the code
of the function, also named “the function body”, between curly braces.
Functions are actions. So
their name is usually a
Our new function can be called by its name
verb. It should be brief, as
accurate as possible and
describe what the
<script> function does,

function msg(){ It is a widespread practice


alert("hello! this is message"); to start a function with a
verbal prefix which
} vaguely describes the
msg(); action.

msg();
</script>
Function Arguments

function getcube(number){
alert(number*number*number);
}
getcube(5);
Default values
If a parameter is not provided, then its value becomes undefined.

function showMessage(from, text = "no text given") {


alert( from + ": " + text );
}
showMessage("Ann"); // Ann: no text given
Function with Return Value
We can call function that returns a value and use it in our program.

function getInfo(){
return "hello john! How r u?";
}
document.write(getInfo());
Local variables
A variable declared inside a function is only visible inside that function.

function showMessage() {
let message = "Hello, I'm JavaScript!"; // local variable
alert( message );
}
showMessage();
alert( message );
Outer Variables
let userName = 'John'; function
A function can access an outer variable as well, for example
showMessage() { userName = "Bob";

let userName = 'John’; let message = 'Hello, ‘ +userName;

function showMessage() { alert(message);

let message = 'Hello, ' + userName; } alert( userName );

alert(message); showMessage();

} alert( userName );

showMessage();

Global variables
Variables declared outside of any function, such as the outer userName in the code above, are called global.
Global variables are visible from any function (unless shadowed by locals).
It’s a good practice to minimize the use of global variables
The “null” value
The special null value does not belong to any of the types described above.
It forms a separate type of its own which contains only the null value:
let age = null;

The “undefined” value


The special value undefined also stands apart. It makes a type of its own, just like null.
The meaning of undefined is “value is not assigned”.
If a variable is declared, but not assigned, then its value is undefined:
Number
The number type represents both integer and floating point numbers.
Besides regular numbers, there are so-called “special numeric
values” which also belong to this data type: Infinity, -
Infinity and NaN.
•Infinity represents the mathematical Infinity ∞. It is a special
value that’s greater than any number.

alert( 1 / 0 ); // Infinity

alert( Infinity );

alert( "not a number" / 2 );


Strings
The expression
A string in JavaScript must be surrounded by quotes inside ${…}
is evaluated and
the result becomes a part
let str = "Hello"; of the string
let str2 = 'Single quotes are ok too’;
let phrase = `can embed another ${str}`;
The syntax of creating string object using new keyword is
given below:

1.var stringname=new String("hello javascript string");

2.document.write(stringname);
Methods Description
charAt() It provides the char value present at the specified index.
concat() It provides a combination of two or more strings
indexOf() It provides the position of a char value present in the given string.
replace() It replaces a given string with the specified replacement.
substr() It is used to fetch the part of the given string on the basis of the specified starting
position and length.
slice() It is used to fetch the part of the given string. It allows us to assign positive as well
negative index.
toLowerCase() It converts the given string into lowercase letter.
split() It splits a string into substring array, then returns that newly created array.
trim() It trims the white space from the left and right side of the string.
charAt(index) Method
var str="javascript"; indexOf(str) Method
document.write(str.charAt(2));
var s1="javascript index test";
var n=s1.indexOf(“test");
concat(str) Method document.write(n);

var s1="javascript ";


var s2="concat example";
toLowerCase() Method
var s3=s1.concat(s2); var s1="JavaScript toLowerCase";
document.write(s3); var s2=s1.toLowerCase();
document.write(s2);
String slice(beginIndex, endIndex) Method

1.var s1="abcdefgh";
2.var s2=s1.slice(2,5);
3.document.write(s2);
split() Method
trim() Method
var str="This is Javascript example";
document.write(str.split(" "));
var s1=" javascript trim ";
var s2=s1.trim(); substr() Method
document.write(s2);
var str="JavaScript";
document.writeln(str.substr(0,4));

You might also like