Most Basics In Javascript

Mahmudul Hasan
3 min readMay 5, 2021
Photo by James Harrison on Unsplash

While talking about programming, one thing appears mostly and that is javascript. Javascript is one of the most used programming language all over the world.Almost 60–70% web application is using javascript some how.Now let’s talk about the most basics of javascript.

Data Types

JavaScript variables can hold many data types: numbers, strings, objects, Array, Boolean, Functions and also Undefined, null. Array is a special kind of object.

1:- charAt()

The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.

const sentence = ‘The Car hit the dog.’;

const indexNum= 4;

console.log(`The character at index ${index} is ${sentence.charAt(indexNum)}`);
// output: “The character at index 4 is C”

2:- concat()

The concat() method is used to join two or more arrays togather.

var p1= [“Mimi”, “Simi”];
var p1= [“Jimi”, “Tom”];
var persons= p1.concat(p2); // console.log(persons)= Mimi,Simi,Jimi,Tom

3:- slice()

The slice() method returns the selected elements in an array, as a new array object.

var items= [“Banana”, “Orange”, “Lemon”, “Apple”, “Mango”];
var myItem= items.slice(1, 3); //console.log(myItem)= Orange,Lemon

Array

1:- array.length

array.length: array.length is used to find out the length of an array.

var items= [“Banana”, “Orange”, “Lemon”, “Apple”, “Mango”];

console.log(foods.length)// The output will be 5.

2:- array.pop()

This method is used to remove the last element from the array.

let foods = [“Apple”,”Orange”,”Banana”,”Mango”,”Strawberry”];

let pop = foods.pop();
console.log(pop);.//
Strawberry removed from the last index.
console.log(foods);//
[“Apple”, “Orange”, “Banana”, “Mango”]

3:- array.push()

array.push() does the opposite of the pop() method. It inserts an element in the last index of the array.

foods.push(“Grape”);
console.log(foods);//
[“Apple”, “Orange”, “Banana”, “Mango”, “Grape”

4. array.shift()

This method removes an element from the first index of the array.

foods.shift();
console.log(foods);//
[“Orange”, “Banana”, “Mango”, “Grape”]

5. Array.unshift()

unshift() method takes one or more items as a parameter and adds them at the first of the array.

foods.unshift(“Guava”);
console.log(foods);//
[“Guava”, “Orange”, “Banana”, “Mango”, “Grape”]

6. some()

some() method is used to test if any element of an array satisfy a test function. If at least one element satisfies the passed test function returns true. If no element satisfy the passed test function returns false.

const ages = [11, 13, 15, 20]
console.log(ages.some( age => age > 12)) // true
console.log(ages.some( age => age > 21)) // false

7. filter()

filter() method is used to get the elements of an array satisfying a test function. Returns a new array with the elements that pass the test function. Returns an empty array if no element pass the test function.

const ages = [11, 13, 15, 20]
const minors = ages.filter(age => age <= 18)
const middleAged = ages.filter(age => age > 45)
console.log(minors) // [ 11, 13, 15 ]
console.log(middleAged) // []

Operator

8. function()

A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when “something” invokes it (calls it).

function myFunction(a, b) {
return a * b;
}
const result = myFunction(3,4)
console.log(result)// will be 12

Events

9. js Events

JavaScript lets web execute code when events are detected.HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.

some events are:- onChange, onClick, onBlur, onmousehover, onmousechange, etc.

10 Javascript Loops

Loops are handy, if you want to run the same code over and over again, each time with a different value.

  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object
  • for/of - loops through the values of an iterable object
  • while - loops through a block of code while a specified condition is true
  • do/while - also loops through a block of code while a specified condition is true

--

--