JS Part 2pdf
JS Part 2pdf
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
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,
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 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";
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;
alert( 1 / 0 ); // Infinity
alert( Infinity );
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);
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));