Thinking in a JavaScript Way

Nu Shohel
6 min readMay 5, 2021

JavaScript is a very common and most popular word in the world of programming language. IN terms of language JavaScript is a machine language. So if anyone wishes to communicate with the machine he/she must have to know how JavaScript speaks. though there are many languages out there. But today I’m going to describe some words of JavaScript and those words communicate with the machine.

JavaScript Code Snippet

Well, JavaScript is a multi-paradigm, dynamic language with types and operators, standard built-in objects, and methods. Its syntax is based on the Java and C languages — many structures from those languages apply to JavaScript as well. JavaScript supports object-oriented programming with object prototypes, instead of classes. for today I’d try to elaborate my understanding of some JavaScript Building Block which are,

  1. Number
  2. String
  3. Array

Number:

in real-world young to old all are familiar with numbers. In JavaScript, there is a word called number which represents every kind of number that are used in the real world like integer, float, hexadecimal, octal or exponential value. The main difference between JavaScript number function and the real-world number is that In real-world number can be used as a number. But in JavaScript Number can be used at the same time as a number and as a word. So, if you want to do some calculation via javascript you have to be careful about what you’re providing. Confused huh!!! Okay, let me explain. If you want to what is 2+2? you need to write 2+2 and it will return 4. But if you write ‘2’+ ‘2’ it will concatenate two numbers as a word and the result will be 22 as a word. however, in javascript there are more properties and methods than only numbers. let's explore some of them.

JavaScript Number Constants

MIN_VALUE: The Number.MIN_VALUE property belongs to the static Number object. It represents constants for the smallest possible positive numbers that JavaScript can work with.For example

console.log(Number.MIN_VALUE); // the result will be 5e-324

MAX_VALUE: In JavaScript, MAX_VALUE is a static property of the Number object that is used to return the maximum numeric value that can be represented in JavaScript. Because MAX_VALUE is a property of the Number object, it must be invoked through the object called Number. For example:

console.log(Number.MAX_VALUE); // Out put will be 1.7976931348623157e+308

NaN: In JavaScript, NaN is a static property of the Number object that is used to return the value that represents Not-A-Number (ie: NaN). Because NaN is a property of the Number object, it must be invoked through the object called Number. For example:

console.log(Number.NaN); // NaN

JavaScript Number Methods

Number parseFloat() method: In JavaScript, parseFloat() is a Number method that is used to parse a string and return its value as a floating-point number. Because parseFloat() is a method of the Number object, it must be invoked through the object called Number. for Example,

console.log(Number.parseFloat('123ABC4'));// will return 123
console.log(Number.parseFloat('ABC123')); // will return NaN

Number parseInt() method: In JavaScript, parseInt() is a Number method that is used to parse a string and return its value as an integer number. Because parseInt() is a method of the Number object, it must be invoked through the object called Number.

console.log(Number.parseInt('35.6'));// will return 35.6 as number

Javascript Math Funcion

Math.floor(): In JavaScript, floor() is a function that is used to return the largest integer value that is less than or equal to a number. In other words, the floor() function rounds a number down and returns an integer value. Because the floor() function is a static function of the Math object, it must be invoked through the placeholder object called Math. for example,

Math.floor(5.95);// will output 5

Math.random(): In JavaScript, random() is a function that is used to return a pseudo-random number or random number within a range. Because the random() function is a static function of the Math object, it must be invoked through the placeholder object called Math. For Example,

Math.floor(Math.random()*10); //returns a random integer from 0 to 9

Math round(): In JavaScript, round() is a function that is used to return a number rounded to the nearest integer value. Because the round() function is a static function of the Math object, it must be invoked through the placeholder object called Math. For example,

Math.round(2.5); // returns nearest interger which is 3
Math.round(2.49); // will return 2

Math sqrt(): In JavaScript, sqrt() is a function that is used to return the square root of a number. Because the sqrt() function is a static function of the Math object, it must be invoked through the placeholder object called Math.For Example,

console.log(Math.sqrt(25));// returns 5

String:

The JavaScript String object is a global object that is used to store strings. A string is a sequence of letters, numbers, special characters, and arithmetic values or combinations of all. Today I’ll try to discous common String method.

String.charAt(): In JavaScript, charAt() is a string method that is used to retrieve a character at a specific position in a string. Because the charAt() method is a method of the String object, it must be invoked through a particular instance of the String class.

var example = 'TechOnTheNet';
console.log(example.charAt(3));// will return h

String.concat(): In JavaScript, concat() is a string method that is used to concatenate strings together. The concat() method appends one or more string values to the calling string and then returns the concatenated result as a new string. Because the concat() method is a method of the String object, it must be invoked through a particular instance of the String class. For example,

var example = 'Tech';
console.log(example.concat(' ','On','The','Net'))// returns Tech on the net;

String toLowerCase(): In JavaScript, toLowerCase() is a string method that is used to convert a string to lowercase. Because the toLowerCase() method is a method of the String object, it must be invoked through a particular instance of the String class.

var example = 'TechOnTheNet';

console.log(example.toLowerCase()); //returns techonthenet

String toUpperCase(): In JavaScript, toUpperCase() is a string method that is used to convert a string to uppercase. Because the toUpperCase() method is a method of the String object, it must be invoked through a particular instance of the String class.

var example = 'TechOnTheNet';

console.log(example.toUpperCase());// returns TECHONTHENET

Array

Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed. Since an array’s length can change at any time, and data can be stored at non-contiguous locations in the array, JavaScript arrays are not guaranteed to be dense; this depends on how the programmer chooses to use them. In general, these are convenient characteristics; but if these features are not desirable for your particular use, you might consider using typed arrays.

Array.concat(): In JavaScript, concat() is an Array method that is used to concatenate two or more arrays into a new array. Because the concat() method is a method of the Array object, it must be invoked through a particular instance of the Array class.

var example1 = ['front','end'];
var example2 = ['developer','blog'];

console.log(example1.concat(example2));//['returns ''front ''end ''developer ''blog']

Array find(): In JavaScript, find() is an Array method that is used to return the value of the first element in the array that meets a specific criteria. Because the find() method is a method of the Array object, it must be invoked through a particular instance of the Array class.

const array1 = [5, 12, 8, 130, 44];const found = array1.find(element => element > 10);console.log(found);// expected output: 12

Array .filter():The filter() method returns an array containing elements of the parent array that match the set test. A function containing a test is passed as an argument to the filter method. To keep an element the test function should return true and false to discard an element.

[10, 2, 5, 100, 8].filter((number)=> number < 20) // filter array for numbers less than 20

Array .forEach():The forEach() method takes a function that performs an action on each of the elements in this array.

[1, 2, 3, 4, 5].forEach(function(number){
console.log(number * 2)
})
//returns 2,4,6,8,10

Array .map(): The map() method takes a callback method that performs an operation on the elements in the array. It returns a new array containing the resulting values of running the operation on each element. This method does not mutate the calling array.

let newArray = [1, 2, 3, 4, 5].map(function(number){
return number * 2
})
// [2, 4, 6, 8, 10]

--

--