Unit-3.1 Java Script - fun - array
Unit-3.1 Java Script - fun - array
Prepared by
Dr Komarasamy G
Associate Professor (Grade-2)
School of Computing Science and Engineering
VIT Bhopal University
1
Unit-3 Java Script and JQuery
2
Unit-3 Java Script and JQuery
Reference Links
•https://www.w3schools.com/js/js_intro.asp
•https://www.javatpoint.com/javascript-tutorial
3
Introduction to Java Script
What is JavaScript?
• JavaScript is a client-side as well as server side scripting
language that can be inserted into HTML pages and is
understood by web browsers.
• JavaScript is an object-based scripting language which is
lightweight and cross-platform.
• JavaScript is not a compiled language, but it is a translated
language.
• The JavaScript Translator (embedded in the browser) is
responsible for translating the JavaScript code for the web
browser.
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"></p>
<script>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>
</body> Output:
</html> JavaScript Variables
In this example, x, y, and z are variables.
The value of z is: 11
<html>
<body>
<h2>JavaScript Operators</h2>
<p>x = 5, y = 2, calculate z = x + y, and display z:</p>
<p id="demo"></p>
<script>
var x = 5;
var y = 2;
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html> JavaScript Operators
x = 5, y = 2, calculate z = x + y, and display
z:
7
Syntax
new Function ([arg1[, arg2[, ....argn]],] functionBody)
Parameter
• arg1, arg2, .... , argn - It represents the argument used by function.
• functionBody - It represents the function definition.
Method Description
apply() It is used to call a function contains this value and a single
array of arguments.
<!DOCTYPE html>
<html>
<body>
<script>
var add=new Function("num1","num2","return num1+num2");
document.writeln(add(2,5));
</script>
</body> Output:
7
</html>
<!DOCTYPE html>
<html>
<body>
<script>
var pow=new Function("num1","num2","return Math.pow(num1,num2)");
document.writeln(pow(2,3));
</script>
</body>
</html> Output:
8
</script>
Unit-3 Java Script / Dr Komarasamy G 33
JavaScript Arrays
3) JavaScript array constructor (new keyword)
• Here, you need to create instance of array by passing arguments in
constructor so that we don't have to provide value explicitly.
The example of creating object by array constructor is given below.
<script>
var emp=new Array("Jai","Vijay","Smith");
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
Output:
</script> Jai
Vijay
Smith
https://www.javatpoint.com/javascript-array
Methods Description
concat() It returns a new array object that contains two or more merged
arrays.
copywithin() It copies the part of the given array with its own elements and
returns the modified array.
entries() It creates an iterator object and a loop that iterates over each
key/value pair.
every() It determines whether all the elements of an array are satisfying
the provided function conditions.
flat() It creates a new array carrying sub-array elements concatenated
recursively till the specified depth.
flatMap() It maps all array elements via mapping function, then flattens the
result into a new array.
fill() It fills elements into an array with static values.