• We just launched and are currently in beta. Join us as we build and grow the community.

How to Get the First Value of an Array in JavaScript

farokmrini

Social Media Maven
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
46
Likes
141
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
How to Get the First Value of an Array in JavaScript

Introduction

In this tutorial we will create a How to Get the First Value of an Array in JavaScript. This tutorial purpose is to teach you how get the first value of an array object. This will cover all the important functionality that will get your first array value. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want to get the first array value. I will give my best to provide you the easiest way of creating this program Get the First Array Value. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html table and buttons for submission. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Get the First Value of an Array</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-6"

    >
  18. <table

    class

    =

    "table table-bordered"

    >
  19. <thead

    class

    =

    "alert-info"

    >
  20. <tr

    >
  21. <th

    >

    Arrays</

    th

    >
  22. </

    tr

    >
  23. </

    thead

    >
  24. <tbody

    id

    =

    "result"

    >
  25. </

    tbody

    >
  26. </

    table

    >
  27. </

    div

    >
  28. <div

    class

    =

    "col-md-1"

    ></

    div

    >
  29. <div

    class

    =

    "col-md-5"

    >
  30. <button

    class

    =

    "btn btn-primary"

    id

    =

    "get"

    >

    Get First Data</

    button

    >
  31. <br

    /

    ><br

    /

    >
  32. <div

    id

    =

    "display"

    ></

    div

    >
  33. </

    div

    >
  34. </

    div

    >
  35. <script

    src

    =

    "script.js"

    ></

    script

    >
  36. </

    body

    >
  37. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to ge the first value of an array when you clicked the button. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. const

    arrays =

    [

    "PHP"

    ,

    "JavaScript"

    ,

    "Python"

    ,

    "C#"

    ,

    "C++"

    ]

    ;


  2. const

    btn_click=

    document.getElementById

    (

    "get"

    )

    ;

  3. btn_click.addEventListener

    (

    "click"

    ,

    getFirstData.bind

    (

    this

    ,

    arrays)

    )

    ;

  4. document.addEventListener

    (

    'DOMContentLoaded'

    ,

    (

    )

    =>

    {
  5. displayData(

    arrays)

    ;
  6. }

    )

    ;


  7. function

    displayData(

    array)

    {
  8. let html=

    ""

    ;
  9. for

    (

    var

    i=

    0

    ;

    i<

    array.length

    ;

    i++

    )

    {
  10. html +=

    "<tr>"

    ;
  11. html +=

    "<td>"

    +

    array[

    i]

    +

    "</td>"

    ;
  12. html +=

    "</tr>"

    ;
  13. }

  14. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    html;
  15. }


  16. function

    getFirstData(

    array)

    {
  17. let data =

    array[

    0

    ]

    ;
  18. document.getElementById

    (

    'display'

    )

    .innerHTML

    =

    "<center><label>Your first array data: <span class='text-primary'>"

    +

    data+

    "</span></label></center>"

    ;
  19. }

In the code above we just simply create one method called getFirstData(), this function will immediately get the first value of your array object. The trick for this function is that we only get the index position of that array value which is 0.

Output:

How%20to%20Get%20the%20First%20Value%20of%20an%20Array%20in%20JavaScript%201.png

The How to Get the First Value of an Array in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Get the First Value of an Array in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

337,656

337,664

Top