JavaScript Matrix17 Mar 2025 | 6 min read We will study the JavaScript matrix in this article. Let us first comprehend about array and matrix. We all know how to create a dimensional array but most of us do not know how to create a 2-dimensional array so in this article we will learn how to construct an array of 2-dimensional in JavaScript. ArrayAn array is a variable that can hold more than one value of the same type. The array can be one-dimensional, two-dimensional, three-dimensional, etc. Example: The above array named "cricketers" is a one-dimensional array that contains the name of cricketers only which means an array can hold the value of the same type of data. Constructing an array in JavaScript:We can create an array in JavaScript by utilizing array literal. Syntax: In the above-provided syntax, the "var" is the keyword that is utilized to create an array that can be initialized at any time. The "arrayName" is the name of the array. The element1, element2 and element3 are the same kind of elements present in the array. MatrixA matrix is a rectangular array that consists of rows and columns. It is similar to a table or grid that contains rows and columns. Dimensions of matrixFollowing are the examples that show the dimensions of a matrix: Example-1: The above matrix contains one row and two columns which means this matrix has a dimension of (1 x 2). Example-2: ![]() The above matrix contains two rows and three columns which means this matrix has a dimension of (2 x 3). Matrix in JavaScriptIn JavaScript, the matrix is also called an array of arrays or multidimensional array. Operations of MatrixLet us first consider the following matrix on which we will perform all operations. Following are the operations that are performed by the matrix: 1. Row: It is utilized to return the row.
It returns [9, 1, 5]. 2. Column: It is utilized to return the column.
It returns [[9], [3], [4]]. 3. Identity:
It returns [[9, 1, 5], [3, 2, 5], [4, 8, 6]]. 4. Range:
It returns [[3, 2, 5], [4, 8, 6]]. 5. Set:
It returns [[0, 0, 0], [3, 2, 5], [4, 8, 6]]. 6. Size: It is utilized to find the size of the matrix.
It returns [3, 3]. 7. Addition: It is utilized to add two matrices.
It adds matrix B and matrix C. 8. Subtraction: It is utilized to subtract one matrix from another.
It subtracts matrix B from matrix C. 9. Multiplication: It is utilized to multiply two matrices.
It multiplies matrices B and C. 10. Division: It is utilized to divide one matrix by another.
It divides matrix B by matrix C. 11. Product: It is utilized to calculate the product of two matrices.
It returns the product of matrix B and matrix C. 12. Transpose: It is utilized to find the transpose of a matrix.
It transposes the matrix B. 13. Inverse: It is utilized to inverse a matrix.
It inverses the matrix C. 14. Determinant: It is utilized to calculate the determinant of a matrix.
It returns the determinant of B. 15. Merge: Two matrices can be merged in the top, bottom, left and right directions. Top direction: It merges the matrix in the top direction. Bottom direction: It merges the matrix in the bottom direction. Left direction: It merges the matrix in the left direction. Right direction: It merges the matrix in the right direction. 16. Equals: It is utilized to check whether the two matrices are equal or not. Demonstrations of JavaScript MatrixLet us see demonstrations for some of the matrix operations in JavaScript. Demonstration-1:We will construct a program for adding matrix A and matrix B. Code: Output: We can witness a resultant matrix which is the addition of matrix A and matrix B. ![]() Demonstration-2:We will build up a program to subtract matrix B from matrix A. Code: Output: We can witness a resultant matrix which is the subtraction of matrix B from matrix A. ![]() Demonstration-3:We will construct a program to multiply matrix A and matrix B. Code: Output: Here in the outcome we can witness a resultant matrix which is the multiplication of matrix A and matrix B. ![]() Demonstration-4:We will construct a JavaScript program to divide matrix C by matrix D. Code: Output: Here is the outcome in which we can have a gaze at the resultant matrix which is the division of matrix C by matrix D. ![]() Conclusion:We have understood the JavaScript Matrix in this article. A matrix is a two-dimensional array that contains rows and columns. There are many operations that can be performed on matrices and we have discussed some of the operations with the assistance of demonstrations. Next TopicJavascript-join |
In this section, we will discuss the print() method in the JavaScript language. A print() method is used to print the currently visible contents like a web page, text, image, etc., on the computer screen. When we use a print() method in JavaScript and execute...
4 min read
Recursion in JavaScript is a programming method wherein a function calls itself at once or indirectly to resolve a problem. Recursion is an effective concept that helps function to be defined in terms of themselves which allows elegant solutions to certain styles of issues. There are some...
7 min read
Difference Between Static and Const in JavaScript We make use of both static and const variables in different languages. In this section, we will explore the distinctions between static and const variables across different programming languages. What is a Static variable in JavaScript A static variable is a...
4 min read
JavaScript is a flexible and strong programming language that powers the intuitiveness of the web. Whether you're a carefully prepared designer or simply beginning your process in programming, practicing JavaScript routinely is fundamental for levelling up your abilities and staying updated with the most recent...
12 min read
The JavaScript Arrow function does not have the "this" value or any argument. Using it as an event handler, an object literal method, or a prototype method is not recommended. If a function applies the arguments objects, the arrow function does not work correctly. The...
4 min read
What is Bootstrap in JavaScript? Bootstrap is a framework in JavaScript that is used for faster and easier web development. Bootstrap in JavaScript includes HTML and CSS-based design templates for the typography, forms, buttons, tables, navigation, modals, and image carousels, as well as optional JavaScript plugins. Bootstrap is...
6 min read
JavaScript functions enable us to perform a variety of tasks, make crucial choices, perform computations, and even enhance the interactivity of our websites. We shall discover the distinction between "function expression" and "function declaration" in this post. Both use the term function, which is where they...
3 min read
We will understand the pyramid in JavaScript in this article. Let us first comprehend the pyramid. What is a pyramid? A pyramid is a triangular structure but in JavaScript, a pyramid is a pattern or sequence of numbers that are arranged in the shape of a triangle. We...
6 min read
Sorting is the way in which ordering of elements are presented according to some criteria. In JavaScript, elements of array are sorted, which can be string or number. Sorting makes data more accessible, helps in searching and enables fast access of search. Types of Sorting in JavaScript Alphabetical...
11 min read
The current URL in the web is important for multiple use cases like analytics tracking, dynamic rendering or implementing conditional logic based on the page URL. JavaScript offers some methods and properties that help the developers to manipulate the URL of the current webpage. In this article,...
12 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India