10 crucial things in JavaScript

Samia Farid
6 min readNov 2, 2020

JavaScript is a client-side scripting language. That is, when a user goes to a website, the webserver will send Html code to the user which will include the JavaScript code and the user could look at this code if they want to. The user’s browser then executes the code when the page is processing.

Let’s discuss some JavaScript types and methods:

String

  • A string is a JavaScript type and Strings in JavaScript holds data that can be represented in text form.

Example:

And, the following are methods of string:-

  1. String.charAt()
  • chatAt() is a method of string.
  • The charAt() method of string tries to access an individual character in a string.

Example:

  • The output is s

2. String.includes()

  • includes() is a string method
  • The includes() method in a string checks whether that specific character or a string is within another string or not.
  • Then, it will return ‘true’ or ‘false’ as appropriate.

Example:

  • The output is true

3. String.slice()

  • slice() is a string method.
  • The slice() method extracts a part of a string and returns a new string, without making any changes to the original string.
  • Slice() method has or can take 2 parameters, which is str.slice( beginIndex, endIndex)
  • You should know any index starts from 0 and endIndex param is optional.

beginIndex:-

  • If begin index is given as 3, then the four characters of the string will be chosen to start extracting from.
  • If begin index is given as 0, then the extraction will start from the beginning of the string only.
  • If begin index is more than the string’s length, then it will return an empty string.
  • If begin index is negative, like -3, then it will take the last 3 characters of a string (like str.length — 3).

endIndex:-

  • If end index is given as 3, as it says end index means it will extract till the 3rd character given, without including the 3rd character.
  • If end index is negative, like -4, then it will start counting from back till the last 4th character and extract up to and before the last 4th character.
  • If end index is more than the string’s length, then it will return an empty string.

Important note, the endIndex should not be bigger than beginIndex.

Example:

Number

  • Number is a JavaScript type and Number in JavaScript converts any string with a number to a number

Example:

And, the following are methods of number

4. Number.parseFloat()

  • This Number.parsefloat() is same as the global parseFloat() function. It has the same properties.
  • parseFloat() can take 1 argument and that argument is converted to floating number .
  • If the first non-whitespace character in the argument of parseFloat() is not a number or cannot be converted, then it will return NaN.
  • parseFloat() ignores any whitespace in the argument
  • parseFloat(), other than a plus sign (+) or a minus sign (-), it can or will ignore any invalid character within the argument, which is if it is after the decimal point and return the value properly.
  • If any invalid character is given before the decimal point, it will parse only up to that point and not after

Example:

Math

  • Math is a built-in object that deals with mathematical constant and can manipulate those numbers with its property and methods.

Math only works with the Number type in JavaScript

And, the following are the static methods of Math.

5. Math.abs()

  • This Math.abs() returns the absolute value of a number.
  • It can take only one parameter and that is a number.
  • The output will be NaN if you pass an empty object, an array with more than a number, a non-numeric string, or undefined (empty variable) in Maths.abs()
  • The output will be 0, if you pass a null, empty string or an empty array in Math.abs().

Example:

6. Math.ceil()

  • Math.ceil() is another static method of Math and will always round a number higher than the number given to ceil.
  • It can also take one parameter which is a number.
  • If the parameter is null, the output will 0 and not NaN.

Example:

Array

  • Array is a JavaScript type and an array is a list-like object which can hold multiple values in a single variable.

Example:

7. Array.filter()

  • Array.filter() is an array method and will the new array of data that has matched the requirements by the provided function.
  • The Array.filter() will give a callback function and go over once each of the elements in the array.
  • Then, it will return a new array with elements that matched the requirements in the callback function.
  • The elements which will not match are ignored and skipped.
  • The callback function can have 3 arguments or less.
  • 1 of them is the value of the element
  • Index of the element
  • Last is the array object that’s been passed over

Example:

8. Array.push()

  • Array.push() is an array method and it will add one or more elements to the end of an array.
  • It will return a new array with a new length.

Example:

9. Array.shift()

  • Array.shift() is an array method and this method will remove the first element from an array
  • Then, it will return a new array with a new length

Example:

10. Array.map()

  • Array.map() is a method of an array and this method provides a callback function once on each of the element in the array
  • The output will be a new array with results provided by the callback function.

Example:

--

--