3 Important JavaScript Topics That You Have To Know About
JavaScript String
A JavaScript string stores a series of characters like “John Doe”. A string can be any text inside double or single quotes
1. slice()
To extract part of a string, use the slice() method. This method returns a portion of a string. The First will be 0 then 1.2….
For example, from ‘https://www.google.com’ we want to keep just www.google.com so it will be-
var url = “www.google.com”;
var domain = url.slice(4); //google.com
2. split()
The split() method is used to split a string into an array of substrings and returns the new array. The split() method does not change the original string.
For example:
var str = “How are you doing today?”;
var res = str.split(“ “);
console.log(res); // [ ‘How’, ‘are’, ‘you’, ‘doing’, ‘today?’ ]
3. indexOf()
JavaScript provides several techniques for searching for a word, number, or other series of characters inside a string indexOf() is one of them. wherein the string a specified value occurs indexOf() will find it.
For example:
var str = “Hello JavaScript lover”;
var n = str.indexOf(“JavaScript”); // 6
4. toLowerCase()
The toLowerCase() method converts a string to lowercase letters. The toLowerCase() method does not change the original string.
For example:
var str = “Hello World!”;
var res = str.toLowerCase();
console.log(res); // hello world!
JavaScript Number
Numbers are very important in JavaScript programming. You can use numbers to keep track of how many times a visitor has visited a web page. In JavaScript, a number is represented by a numeric character; 5, for example, is number five. You can also use fractional numbers with decimals, like 5.42 or 32.0000231. JavaScript even lets you use negative numbers, like -4, -100, etc.
1. isNaN()
The isNaN() function determines whether a value is an illegal number (Not-a-Number). This function returns true if the value equates to NaN. Otherwise, it returns false. This function is different from Number.isNan() method.
For Example:
console.log(isNaN(‘hello’)); //output: true;
console.log(isNaN(23)); //ouput: false;
2. parseFloat()
When you create a variable, you can store a number in it like this:
Var a = 3.43;
However, there are times when a number is actually a string. For example, if you use prompt() method to get visitor input, even if someone types 3.43, you will end up with a string that contains a number.in other words, the result will be ‘3.43’(a string) and not 3.43 (a number).so we have to convert from string to float for doing our task.
For example:
var c = parseFloat(“10.33”); // value of c will be 10.33
3. parseInt()
Same as parseFloat() but parseInt() will convert the string into integer .
For example:
var c = parseInt(“10”); // value of c will be 10.
JavaScript Math
JavaScript Math object will allow you to perform a mathematical task on a number. It’s not a function object. Math is not a constructor. The math object provides you with properties and methods for mathematical constants and functions.
1. Math.floor()
The Math.floor() function will return the largest integer less than or equal to a given number like if you give 10.9 it will return 10 as an output.
For Example:
console.log(Math.floor(11.4))
Output: 11
Console.log(Math.floor(3.91))
Output: 3
2. Math.random()
The Math.random() function will returns a random number between range [0,1) , 0 (inclusive) and 1 (exclusive). It always returns a number lower than 1.
For Example:
console.log(Math.random());
Output: between 0 to lower than 1
console.log(Math.random()* 10);
output: between 0 to lower than 10
3. Math.round()
The Math.round() function returns the value of a number rounded to the nearest integer.
For example:
console.log(Math.round(5.5), Math.round(5.9), Math.round(5.4));
output: 6, 6, 5