Wednesday, April 23, 2025

JavaScript Arrays

An array is a special type of object that stores a collection of values of any type (numbers, strings, objects, etc.). Arrays are created using square brackets [] and elements are separated by commas. Each element in an array has an index, which is a numerical value that starts from 0 for the first element, 1 for the second element, and so on. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>

<body>

<script>
	
	var plang = new Array("PHP", "Python", "C++", "C");
	
	document.write(plang);
	
</script>
	
</body>
</html>

The code creates an array plang containing four programming languages: "PHP", "Python", "C++", and "C". Then, the code outputs the entire array using the document.write() function. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>

<body>

<script>
	
	var plang = new Array("PHP", "Python", "C++", "C");
	
	document.write(plang[0] + "<br>");
	document.write(plang[1] + "<br>");
	document.write(plang[2] + "<br>");
	document.write(plang[3] + "<br>");
	
</script>
	
</body>
</html>

This script uses the document.write() method to output each element of the array on a new line. The [0], [1], [2], and [3] in plang[0], plang[1], plang[2], and plang[3] are called indexes and are used to access each element of the array.

The first element in an array has an index of 0, the second has an index of 1, and so on. In this script, plang[0] is "PHP", plang[1] is "Python", plang[2] is "C++", and plang[3] is "C".

Indexes in JavaScript start from 0 and in most other programming languages.

There are several programming languages where indexes start with 1. Some examples are:

  • MATLAB: MATLAB is a numerical computing environment and programming language used in scientific research, engineering, and other fields. In MATLAB, array indexing starts from 1.

  • FORTRAN: FORTRAN is a high-level programming language used primarily for scientific and engineering applications. In FORTRAN, array indexing also starts from 1.

  • COBOL: COBOL is a business-oriented programming language used in finance, insurance, and other industries. In COBOL, array indexing starts from 1.

However, it's worth noting that many popular programming languages such as C, Java, and Python, use 0-based indexing for arrays and other data structures.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...