MCITMC - Tin Tức Việt Nam chính xác, cập nhật trực tiếp 24/7
  • Home
  • Bất Động Sản
  • Công Nghệ
  • Kinh Doanh
No Result
View All Result
  • Home
  • Bất Động Sản
  • Công Nghệ
  • Kinh Doanh
No Result
View All Result
MCITMC - Tin Tức Việt Nam chính xác, cập nhật trực tiếp 24/7
No Result
View All Result

Arrays in javascript

admin by admin
February 3, 2020
in Công Nghệ
15
Arrays in javascript



Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Arrays are collections and ZERO indexed. This means the first element is at index ZERO and the last element is at index arrayObject.length – 1. length property of the array object returns the size of the array.

The following JavaScript code creates an empty array. length property returns 0 in this case.

var emptyArray = [];
alert(emptyArray.length);

Output : 0

Another way to create an array is by busing the Array constructor as shown below. In this example we are setting the length of the array to 10.

var myArray = new Array(10);
alert(myArray.length);

Output : 10

Retrieving first and last elements from the array using the array index

var myArray = [10, 20, 30];
document.write(“First element = ” + myArray[0] + “[br/]”);
document.write(“Last element = ” + myArray[myArray.length – 1] + “[br/]”);

Output :
First element = 10
Last element = 30

Populating an array in JavaScript : There are several ways to populate an array in JavaScript. Let’s look at those different option now.

Declaring an array first and then populating using the array index

var myArray = [];

myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;

alert(myArray);

Output : 10, 20, 30

Declaring and populating the array at the same time

var myArray = [10, 20, 30];
alert(myArray);

Output : 10, 20, 30

Declaring an array first using the Array constructor and then populating using the array index. Though the initial size is set to 3, adding a fourth element to the array will not throw an exception, because arrays in JavaScript can grow in size.

var myArray = new Array(3);

myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;

alert(myArray);

Output : 10, 20, 30

Declaring and populating the array at the same time using the Array constructor

var myArray = new Array(10, 20, 30);
alert(myArray);

Output : 10, 20, 30

Please note : If only one number is passed to the Array constructor, then that number is used to set the size of the array. If more that one number is passed then those will be used as elements to populate the array.

A for loop can be used to populate and retrieve elements from the array object in JavaScript

Populating an array using for loop : The following JavaScript code stores even numbers in the array from 0 to 10. Notice that we are using a for loop to populate the array.

var evenNumbersArray = [];
for (var i = 0; i [= 5; i++)
{
evenNumbersArray[i] = i * 2;
}
alert(evenNumbersArray);

Output : 0,2,4,6,8,10

Retrieving elements from the array using for loop :

var evenNumbersArray = [];
for (var i = 0; i [= 5; i++)
{
evenNumbersArray[i] = i * 2;
}

for (var i = 0; i [ evenNumbersArray.length; i++)
{
document.write(evenNumbersArray[i] + “[br/]”);
}

Output :
0
2
4
6
8
10

Nguồn:https://mcitmc.org/

Xem Thêm Bài Viết Khác:https://mcitmc.org/cong-nghe/

Tags: Youtube
Previous Post

TOP 100 MẪU ÁO DÀI HOA NHÍ ĐẸP NHẤT 💝 Vải Áo Dài S

Next Post

Bản đồ quy hoạch huyện nhà bè thành phố Hồ Chí Minh

Next Post
Bản đồ quy hoạch huyện nhà bè  thành phố Hồ Chí Minh

Bản đồ quy hoạch huyện nhà bè thành phố Hồ Chí Minh

Comments 15

  1. sandeep kumar Brungi says:
    2 years ago

    can u explain why i'm getting finally undefined next to degit 10. when i use <= in myevenNumbersArray.length (for loop)

    Reply
  2. prabu mech says:
    2 years ago

    One of the best videos ever

    Reply
  3. Beatrix Ducz says:
    2 years ago

    This is completely vica verse syntax than C# lol (brackets)

    Reply
  4. Stationery Guide says:
    2 years ago

    Nicely explained sir

    Reply
  5. HybridFlu says:
    2 years ago

    You explained it better than a paid course on Udemy.

    Reply
  6. Doodle Doo says:
    2 years ago

    I like how you whisper in my ears.

    Reply
  7. Anupam says:
    2 years ago

    is multi dimension array not possible in javascript?

    Reply
  8. Antony Allocious says:
    2 years ago

    document.getElementById('demo4').innerHTML = populateArray + '<br>';
    When i am trying to get it through getElementById means, i couldnt get a result like below code

    document.write(populateArrayfor[i] + "<br>")

    Reply
  9. Dibyayan Bhattacharjee says:
    2 years ago

    Amazing

    Reply
  10. Chaaos2 says:
    2 years ago

    Appreciate the video, you answered my exact question i came here for at 2:40 with good information all along!

    Reply
  11. Moses Dwana says:
    2 years ago

    thanks very much sir

    Reply
  12. Manoj Karthik says:
    2 years ago

    Thanks Alot Sir… This tutorial Cleared my arrays concept…Very helpful video for beginners like me.

    Reply
  13. Abdullah Semih AYYILDIZ says:
    2 years ago

    Thx again master..

    Reply
  14. Nagendra Babu says:
    2 years ago

    sir, i know this is JavaScript Tutorial, I have one doubt related to SQL. Why we can not use Stored Procedure in Function. Please Answer when you are free. 

    Reply
  15. Mr Irrepressible says:
    2 years ago

    Thanks for the video. Could u pls explain some advanced array techniques such as doing arithmetic with the elements of an array eg adding the 1st two elements together and then comparing the sum to the next element in the array? Thanks

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Xem Thêm

Công trình này của huyện Đông Anh đã cơ bản hoàn thiện.

Toàn cảnh 4 dự án hạ tầng trăm tỷ đồng đang thành hình ở huyện Đông Anh

June 29, 2022
CĂN HỘ WEST GATE

CĂN HỘ WEST GATE

June 29, 2022

Dự án chung cư Sun Square

June 20, 2022

Đánh giá quận Nam Từ Liêm

June 15, 2022
Dự án chung cư sắp mở bán tại TPHCM mới nhất 2022

Dự án chung cư sắp mở bán tại TPHCM mới nhất 2022

March 5, 2022
Chủ đầu tư Opal City View phát triển dự án theo hướng nào?

Chủ đầu tư Opal City View phát triển dự án theo hướng nào?

July 9, 2021
  • Liên Hệ
  • Chính Sách Bảo Mật

© 2022 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Bất Động Sản
  • Công Nghệ
  • Kinh Doanh

© 2022 JNews - Premium WordPress news & magazine theme by Jegtheme.