JavaScript Recap
Hi, today I’m here to recap some JavaScript fundamentals.
Let's begin,
What is JavaScript?
JavaScript is a scripting or programming language that is the world’s most popular programming language that allows you to implement complex features on web pages. JavaScript can update and change both HTML and CSS. JavaScript can calculate, manipulate and validate data.
The Math Object
Unlike other objects, the Math object has no constructor.
The Math object is a static object.
All methods and properties can be used without creating a Math object first.
Math.ceil()
Math.ceil(x)
returns the value of x rounded up to its nearest integer:
Example
Math.ceil(4.9); // returns 5
Math.ceil(4.7); // returns 5
Math.ceil(4.4); // returns 5
Math.ceil(4.2); // returns 5
Math.ceil(-4.2); // returns -4
Math.floor()
Math.floor(x)
returns the value of x rounded down to its nearest integer:
Example
Math.floor(4.9); // returns 4
Math.floor(4.7); // returns 4
Math.floor(4.4); // returns 4
Math.floor(4.2); // returns 4
Math.floor(-4.2); // returns -5
Math.sign()
Math.sign(x)
returns if x is negative, null or positive:
Example
Math.sign(-4); // returns -1
Math.sign(0); // returns 0
Math.sign(4); // returns 1
JavaScript Arrays
JavaScript arrays are used to store multiple values in a single variable.
Example
var cars = [“Saab”, “Volvo”, “BMW”];
toString()
The JavaScript method toString()
converts an array to a string of (comma separated) array values.
Example
var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
document.getElementById(“demo”).innerHTML = fruits.toString();
Result:
Banana,Orange,Apple,Mango
Reversing an Array
The reverse()
method reverses the elements in an array.
You can use it to sort an array in descending order:
Example
var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.sort(); // First sort the elements of fruits
fruits.reverse(); // Then reverse the order of the elements
Filter()
The filter() array method creates a new array with elements that goes under a given criteria from an existing array.
Example:
const numbers = [1,3,5,7,9,11,13,15];
const filterNumber = numbers.filter(number => number > 3);
console.log(“The filter number is: “,filterNumber);
Map()
The map() is a collection of elements where each element is stored as a key, value pairs.
Example:
const number = [4, 8, 12, 24, 48];
const newNumber = number.map(x => x * 2);
console.log(“New Number is: “,newNumber);
JavaScript Strings
JavaScript strings are used for storing and manipulating text.
A JavaScript string is zero or more characters written inside quotes.
Example
var x = “John Doe”;
length
The length
property returns the length of a string:
Example
var txt = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
var sln = txt.length;
indexOf()
The indexOf(),
the method returns the index of (the position of) the first
occurrence of a specified text in a string:
Example
var str = “Please locate where ‘locate’ occurs!”;
var pos = str.indexOf(“locate”);
search()
The search()
, the method searches a string for a specified value and returns the position of the match:
Example
var str = “Please locate where ‘locate’ occurs!”;
var pos = str.search(“locate”);