JavaScript

Java Script Functions

A function is a piece of reusable code that can be called from anywhere in the program. Instead of repeating the code the same functionality can be reused if written in a function and called. JavaScript has built-in functions. Customized functions can be written by the programmer. The name of the function should be a valid JavaScript identifier. A function can have one or more parameters or it may not have any parameters at all. The function parameters are separated by commas. When declaring a function parameters are specified, but while calling a function the arguments are passed. They have different names based on the action being performed. When one function is in another function then they are nested functions.
Declare a function:
function fn()(
document.write(“hello”)
}
Call the function:
fn()

Pass the parameters
function fn(x,y){
let z=x*y
document.write(z)
}
Call the function
fn()

Return value from function
function fn(x,y){
let z=x*y
return z
}
Call the function
let p=fn(2,3)
document.write(p)

Some important functions
Concat combines strings.
Var arr1= [“A”,” B”]
Var arr2= [“C”,” D”]
Var res=arr1.concat(arr2)
document.write(res)
Output: A, B, C, D

Substring returns part of string from start index up to end index excluding the end index.
Const str=JavaScript
Const str1=str.substring(4,7)
document.write(str1)
Output:Scr

ChatAt returns the character at the index specified.
Const str=JavaScript
Const str1=str.charAt(8)
document.write(str1)
Output:p

IndexOf returns the first occurrence of the search string.
Const str=” This is one sentence. This is one more sentence.”
Const str1=str.IndexOf(‘one’)
document.write(str1)
Output: 8

Startswith returns true or false whether string starts with the specified string.
Const str=” This is a string”
Const str1=str.startswith(‘This’)
document.write(str1)
Output: true

toUpperCase() returns the string to upper case.
const str=’this’
Const str1=str.toUpperCase()
document.write(str1)
Output: THIS

Trim () removes whitespace from both ends of the string.
Const str=’ This ‘
Const str1=str.trim())
document.write(str1)
Output: This

Replace () replaces the search string with the replacement string.
Const str=’This is the string’
Const str1=str.replace(‘the’,’a’)
document.write(str1)
Output: This is a string

Slice () extracts parts of a string and returns as a new string.
Const str=’This is a string’
Const str1=str.slice(8)
document.write(str1)
Output: a string

Split breaks the string at delimiter (here it is space)
Const str=” This is one string”
Const str1=str.split(‘ ‘)
document.write(str1[2])
Output: one

<!DOCTYPE html>

<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta charset=”utf-8″ />
<title>JS Functions</title>
</head>
<body>

<script>
//Declare function
function fn() {
document.write(“This is Javascript function”)
}
//Call function
fn() //Answer: This is Javascript function

//Declare function that accepts parameters
function fn1(x, y) {
return x * y
}
//Call function with arguments
document.write(“<br>”)
let z = fn1(2, 3)
document.write(z + “<br>”) //Answer:6

//Scope of variable inside function is local to function
let p = “HTML”
function fn2() {
let p = “JavaScript”
document.write(p + “<br>”) //Answer:JavaScript
}
fn2()
document.write(p + “<br>”) //Answer:HTML

</script>
</body>
</html>

Output:
This is Javascript function
6
JavaScript
HTML