Functions, Objects, and Arrays in JAVASCRIPT

IEEE_TEMS BLOGS
7 min readOct 21, 2021

“Javascript is the duct tape of the Internet.” — Charlie Campbell

Previously from IEEE_TEMS Blogs, we gave an introduction to JS and learned about the data types, variables, loops, and conditions in JavaScript and now we are going to learn about how JavaScript works, through the use of functions, arrays, and different methods of string and numbers. In this blog, we will learn about the functions and methods of arrays, numbers, and strings.

Let’s now dive deep into JS with examples.

Java is to JavaScript as ham is to a hamster.

Function

A function is a much-needed part of any programming language it provides us the reusability of code many times, we can define the code once and use it many times, a function is also used many times using different arguments

A function is a “subprogram” that can be called by code outside the function. Like the program itself, a function is composed of a sequence of statements called the function

Values can be passed to a function, and the function will return a value.

A JavaScript function is defined with the function keyword, followed by the function name, followed by parentheses ().

Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).

The function can include arguments in the parentheses (argument1)

The code to be executed, by the function, is placed inside curly brackets {}

And this code inside the brackets is called the statement

Function invocation

A function is executed when it is called or invoked a function is invoked using an event listener or using a block of code

Function return

When we invoked a function using a Javascript code than passing from a statement, JavaScript will “return” to execute the code after the invoking statement.

Functions often compute a return value The return value is “returned” back to the “caller”:

DEMONSTRATIONS WITH EXAMPLES:

Here first we declare a function of name sumnum and then pass the argument as a and b (some random variable) and then we call the function below sumnum(4,6) passing two-argument 4 and 6 then function return the sum value 10 and 10 is visible in the console here we see that we can use the function lot of time using the different argument.

Objects

Objects are also the variables but this variable contains many values. we can say that object can store all types of the data type

Like an object has its properties and methods

We can access any property of the object

Object name.property name

Explanation using example

Here we declare an object person and person has a properties first name, age and address here age is an int data type and the name is string datatype but we store all these in a variable person and that variable works as the object here

Object method

An object also has a method

Methods are the action that we apply to the properties of the object

Methods are stored in properties as the function definition

A method Is a function stored as a property

String methods

1) length:

This method will return the length of the string

2) Slice(start, end):

This function returns a substring of the string based on the “start” and “end” index arguments, NOT including the “end” index itself. “End” is optional, and if none is specified, the slice includes all characters from “start” to the end of the string.

3) Replace:

This method will replace the particular word with the word we want to replace

4) toLowerCase()

This will return the string with all of its characters converted to lowercase.

5) toUpperCase()

This will return the string with all of its characters converted to uppercase.

String search Method

1) String.indexof()

This method will return the position of the first occurrence of the specified letter

2) String.lastIndexof()

This method will return the position or index of the last occurrence of the specified word

3) String.search()

This method searches for the specified word and returns the index of that word from a string

4) String.includes()

Include method will return if a particular word is included in string or not

Number methods

Number methods help you work with numbers.

Nan is a JavaScript reserved word indicating that a number is not a legal number.

  • toString()

This method will convert a number into a string

  • toExponential()

returns a string, with a number rounded and written using exponential notation.

  • toFixed()

returns a string, with the number written with a specified number of decimals:

  • toPrecision()

returns a string, with a number written with a specified length:

Array

Arrays are used to store multiple values in a single variable.

These are the variables that can store multiple values at a time

To declare an array in JavaScript use the let keyword with square brackets and enclose all the elements within them.

Difference between Array and Objects

JavaScript variables can be objects. Arrays are considered to be special kinds of objects. Because of this, you can have variables of different types in the same Array.

In JavaScript, arrays use number indexes Whereas, objects are used as named indexes

JavaScript Array Methods

The purpose of using an array is to store multiple values in a single entity of a declared variable. Arrays are used when we want to access elements in an orderly fashion using a single variable. One can store strings, boolean, and numbers in a single array.

It is easy to remove elements and add new elements while working with arrays.

1) push()–The push() method adds a new element to the end of an array. 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. Pop() returns the value that has been removed and not the array length like Push().

3) shift()– Shifting is similar to popping, working on the first element instead of the last. The shift() method is used to remove the first array element and shift all other elements to a lower index. It will return you the string that has been shifted out. Shift() works the same as pop() but it returns the first element of the array instead of the last one.

4) concat() — The concat() method creates a new array by concatenating or merging existing arrays. It does not modify the existing array and always returns a new array.

5) toString()– The toString() method is used to convert an array to a string or array values, separated by commas.

6) slice() — The slice() method is used to slice out a piece of an array into a new array. It creates a new array without removing any elements from the source array. It will return the value that has been sliced out from the array.

  • reverse()– The reverse() method is used to reverse the order of the elements in an array. It will change the original array and swap the order of the elements.
  • sort() — The sort() method is used to sort an array alphabetically This function sorts the values as strings by default.

JavaScript is a flexible and powerful programming language that is implemented consistently used by various web browsers. JS makes responsive design easier for developers. Either it is a beginner or an expert developer JS has become integral to the internet experience as developers build increased interaction and complexity into their application. Every aspirant developer should stay in touch with the language to make it a profession or to become an expert in JS.

--

--

IEEE_TEMS BLOGS

IEEE_TEMS, a chapter of VIT UNIV, before we tell about us STOP! Make your fingers join forces with your mind to work hard.Let’s go to the glassy world of techs