JavaScript Recap

Avi Mistry
2 min readMay 5, 2021

--

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”);

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Avi Mistry
Avi Mistry

Written by Avi Mistry

HI I'm Avi Mistry . A full stack web developer works with ReactJs, NodeJs, MongoDB, Firebase, Netlify & more ;)

No responses yet

Write a response