Coder Social home page Coder Social logo

jsarrayfunctions's Introduction

Article Summary

JavaScript Array Methods

Examples are:

  • Array length: This property returns the length (size) of an array. e.g let size = fruits.length;

  • Array toString(): This toString() converts an array to a string of (comma separated) array values.e.g fruits.toString();

  • Array pop(): The pop() method removes the last element from an array. e,g fruits.pop()

  • Array push(): The push() method adds a new element to an array (at the end). e.g. fruits.push("Kiwi")

  • Array shift(): The shift() method removes the first array element and "shifts" all other elements to a lower index. e.g. fruit.shift();

  • Array unshift(): The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements. e.g. fruit.unshift("Kiwi");

  • Array splice(): The splice() method adds new items to an array. e.g fruits.splice(2, 0, "Lemon", "Kiwi");

    • The first parameter (2) defines the position where new elements should be added (spliced in).
    • The second parameter (0) defines how many elements should be removed.
    • The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.
  • Array slice(): The slice() method slices out a piece of an array and returns an array with the deleted items.e.g. fruits.splice(2, 2, "Lemon", "Kiwi");

  • Array join(): The join() method also joins all array elements into a string, but in addition you can specify the separator. e,g. fruits.join(" \* ")

  • Array delete(): Using delete leaves undefined holes in the array. e. g delete fruits[0]. Its better to Use pop() or shift() instead.

  • Array concat(): The concat() method creates a new array by merging (concatenating) existing arrays. e.g myGirls.concat(myBoys) , arr1.concat(arr2, arr3); , arr1.concat("Peter") . Note: The concat() method does not change the existing arrays. It always returns a new array.

  • Array flat(): Flattening an array is the process of reducing the dimensionality of an array. e.g const myArr = [[1,2],[3,4],[5,6]]; , const newArr = myArr.flat();


JavaScript Functions

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).
  • 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 Invocation


JavaScript Reference Functions

Functions are first-class objects, because they can be passed to other functions, returned from functions, and assigned to variables and properties. 

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

Return value

  • One function call can only return one value, but you can simulate the effect of returning multiple values by returning an object or array and destructuring the result.

Passing arguments

  • Arguments are always passed by value and never passed by reference. This means that if a function reassigns a parameter, the value won't change outside the function.

Function parameters

Function parameter is a simple identifier that you can access in the local scope.

Defining functions

Broadly speaking, JavaScript has four kinds of functions:

  • Regular function: can return anything; always runs to completion after invocation
  • Generator function: returns a Generator object; can be paused and resumed with the yield operator.
  • Async function: returns a Promise; can be paused and resumed with the await operator
  • Async generator function: returns an AsyncGenerator object; both the await and yield operators can be used

For every kind of function, there are three ways to define it:

  • Declaration
  • Expression
  • Constructor

Block-level functions

  • functions inside blocks are scoped to that block.

JavaScript Control Flow Statements

In JavaScript, there are three main types of control flow statements:
  • if/else statements: The if…else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed.
  • switch statements: Switch statements are used to execute a block of code based on the value of a variable or expression.
  • Loops.
    • For Loops
    • While Loops : A while loop simply repeats itself while something is true. It continues until the condition is false.
    • do ... while loop : The do ... while loop will first execute the code, then continue while the condition remains true.

JavaScript Functions Basics

  • Functions are the main “building blocks” of the program. They allow the code to be called many times without repetition.

To create a function we can use a function declaration.

function showMessage() {
 alert( 'Hello everyone!' );
}
showMessage().

Local, Outer,and Global variables

  • Local variables declared inside a function is only visible inside that function.
  • The function has full access to the outer variable. It can modify it as well.
  • Global variables are visible from any function.

Parameters

  • Parameters are arbitrary data passed to a function.
  • An argument is the value that is passed to the function when it is called (it’s a call time term).

Default values

  • If a function is called, but an argument is not provided, then the corresponding value becomes undefined. e.g showMessage(from, text) called with showMessage("Ann"); l would output *Ann*: undefined"
  • We can specify the so-called “default” (to use if omitted) value for a parameter in the function declaration, using =. e.g showMessage(from, text = "no text given")
  • To check if parameter is missing use ??

Return a value

  • When the execution reaches a return, the function stops, and the value is returned to the calling code.
  • If a function does not return a value, it is the same as if it returns undefined

jsarrayfunctions's People

Contributors

kofodym avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.