Knowing Some Basic Javascript

Ruhana Binte Karim
4 min readMay 5, 2021

--

Strings

Strings in Javascript is actually a data type that is used to store text rather than numbers. Everything we declare in a single quotation or double quotation or backticks are strings. Example:

var string1 = "Helloo";  //text in quotation
var string2 = '1674'; // numbers in quotation
var string = 'false' // booleans in quotation

Some string methods:

  1. charAt(): If you want to access a specific character in a string, you will use this charAt() method.

Example:

var str1 = ‘Hellooo’;var res = str1.charAt(4);Output: o

2. split(): This method will divide a string into substrings. These substrings then will be stored in an array and will be returned as the array object.

Example:

var string = “I am motivated”string.split(“ ”);  Output: Array(3) [“I”, “am”, “motivated” ]  // The empty space is indicating to split into substrings.Another example:
var string = "I am motivated"
string.split("m");
Output: ["I a", " ","otivated" ] //Here it is splited into substrings without that m.

3. slice(): The slice() method is used to divide parts of a string and return the divided parts in a new string. It uses a start and an end parameters to specify the part of the string you want to divide.

string.slice(start , end)

Example:

var string = “I am a developer”string.slice(0, 4)Output: I am  // Here the 0 indicates the starting index which is I and ends just before the index 4 

4. replace(): The replace() method searches a string for a specified value and returns a new string by replacing the previous value.

string.replace(‘specified value’, ‘new string value’)

Example:

var string = ‘Happy Birthday’string.replace(‘Birthday’ , ‘Anniversary’)Output: Happy Anniversary  // Replaced by the second string value.

Array

An array in Javascript is a data structure that contains a list of elements that store multiple values, like strings, numbers, objects, etc under a single variable. Example:

let names = ['Ruhana','Mou','Amina','Eva','Hany','Oishy']
let numbers = [ 1, 4, 8, 3]
let objects = [name = 'Ruhana' , age = 21]

Some array methods:

  1. push(): This method is used to remove elements and add new elements. The push() method adds a new element to the end of an array. The return value is the new array length.

array.push()

Example:

let cart = [‘mobile’, ‘laptop’ , ‘earphone’ ]cart.push(‘charger’)Output: 4   // returns only the array length

N.B: Push() does not return the value that has been added to the array. It only returns the new length of the array.

2. pop(): The pop() method is used to remove the last element from an array. It returns the value that has been popped out.

array.pop()

Example:

let cart = [‘mobile’, ‘laptop’ , ‘earphone’ ]cart.pop(‘earphone’)Output: earphone  // returns the last removed element.

N.B: Pop() returns the value that has been removed and not the array length like Push().

3. map(): The method allows to iterate over an array. Usually we use a loop to iterate over but with the map() method we don’t need to write a loop over again and modify its elements using a callback function. This callback function will then be executed on each of the array's elements.

Example:

Using for loopvar array = [1,2,3,4,5];
for(var i = 0; i < array.length ; i++){
array[i] = array[i] * 3;
}
console.log(array)
Using map()let array = [2,4,6,8];
let res = array.map(function(element){
return element * 2;
})
Output: [4,8,12,16]

N.B: The function in the map() can have three parameters — element, index, and array

4. filter(): The filter() array method creates a new array with elements that fall under a given condition from an existing array.

Example:

let numbers = [3,4,5,6,7];let res = numbers.filter(function(num){return num < 6})Output: [3,4,5] // returns all matching elements

5. find(): The find() method is very much like the filter() method except it only returns the first matching single element but filter() shows all the matching elements. Another difference is when nothing is found, this method returns a value of undefined.

Example:

let numbers = [3,4,5,6,7];let res = numbers.find(function(num){return num < 6});Output: 3   // returns only first matching element

Number

Some Methods used in Number:

  1. parseInt(): This function parses a string argument and returns integer.

Example:

let x = ‘33’console.log(parseInt(x));Output: 33

N.B parseInt() doesn’t perform mathematical calculation.

Example:

let x = ’33 + 20’console.log(parseInt(x));Output: 33

2. parseFloat(): This function parses a string argument and returns a floating-point number.

Example:

let x = ‘33.9843abc’console.log(parseFloat(x));Output: 33.9843   // ignores the abc as they are not numeric

Math —

Math is a built-in object that has properties and methods for mathematical constants and functions. Three methods that are mostly used to round off a number in javascript —

  1. Math.ceil(): The Math.ceil function in JavaScript is used to round up a number that is passed into it to its nearest integer or more specifically to the greater value.

Example:

console.log(Math.ceil(2.44));Output: 3  //returns nearest greater value

2. Math.floor(): This method returns the largest or equal integer that is less than the given value.

Example:

console.log(Math.ceil(2.44));
Output: 2
console.log(Math.ceil(2.67));
Output: 2

3. Math.round(): This method rounds off a number depending on the fractional part of the number.

Example:

console.log(Math.ceil(2.44));Output: 2  // if less than .5 then returns lower valueconsole.log(Math.ceil(2.65));Output: 3   // if greater than .5 then returns nearest greater value

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

--

--

No responses yet

Write a response