# JavaScript

## JavaScript Array, map, valueOf,indexOf and lastIndexOf

The map function of a JavaScript array applies a certain method to all elements and creates an array. Example 1 The following method call will change all values to positive function f() { var a = Array(1,2,-3,4); var b = a.map(Math.abs); } Output: a=1,2,-3,4 b=1,2,3,4 Example 2 The following method call will…

## Array functions concat, copyWithin, every, fill, filter, find, forEach

1. concat function merges arrays and returns the combined array Example 1 var a = Array(1, 2, 3); var b = Array(4, 5, 6); var c=Array(7,8,9); var d = a.concat(b, c); a=1,2,3 b=4,5,6 c=7,8,9 d=1,2,3,4,5,6,7,8,9 concat does not modify the original arrays and we can concat as many arrays as…

## Array functions toString, join, pop, push, shift and unshift

The toString function converts an array to a string by concatenation elements with commas in between. toString method Example 1: var a=Array(1,2,3); var s=a.toString(); s is “1,2,3” Example 2: var a = Array(1, “apple”, Math,Math.sqrt); var s = a.toString(); s is “1,apple,[object Math],function sqrt() { [native code] }” join method…

## Splice function in JavaScript

The splice function in JavaScript is used for adding, removing and returning elements from an array. Let us suppose we have an array var a=new Array(1,2,3,4,5); Splice with one parameter: This returns the elements of the array starting at the parameter. var b=a.splice(0); b=1,2,3,4,5. a=var b=a.splice(2): b=3,4,5 a=1,2 Splice with 2…

## Sort Function in JavaScript Arrays

The sort function in JavaScript Arrays allows performs sorting of array elements. Like all of JavaScript this function is remarkably flexible as well. Let us say we want to sort an array. var a=Array(4,5,3,-2); To sort you call a.sort(); This will sort the array numerically. -2,3,4,5 To change the sorting mode we…

## Arrays in JavaScript

An Array in JavaScript is a collection of items. The items can be accessed and modified. The whole array can be passed as a parameter to some function. Since JavaScript is typeless we can store data of any kind. The array automatically creates a variable called length which automatically gives the size…

## Loops in JavaScript

JavaScript supports the 3 loops normally used in programming. The for loop, while loop and the do while loop. ForLoop Consider the following for loop: var output=””; for(var i=1;i<=10;i++) output=output + “,” + i; The four parts of this loop are: var i=1; Loop Initialization i<=10; Loop Condition output=output + “,” +…

## Scoping of Variables and Nested Functions

JavaScript has two scopes local and global.  Variables inside a function are local if defined with the prefix var, if not it becomes global. Consider the following code: function f1() { x = 10; } function f() { f1(); x = x + 5; alert(x); } If I call the function…

## Functions in JavaScript

Functions in JavaScript bring out the real flexibility of the language. Functions can be stored in variables, defined at runtime. Even inbuilt functions can be redefined. Declaring functions function add(a,b) { return a+b; } a and b are the 2 parameters. I can call this function in the following way add(3,4).…

## Mathematical functions and constants in JavaScript

This is post is about mathematical functions and constants in JavaScript. Most functions are part of the Math object. The important constants and functions of the Math object are. Constants Math.E   gives Euler’s Number 2.718281828459045 MathLN2  gives natural log of 2 0.6931471805599453 Math.LN10 gives natural log of 10 2.302585092994046…