Comparison of Javascript and MATLAB

Javascript is the default programming language used by web browsers. You can see below that the general logic is the same but the syntax is different. Most importantly, MATLAB makes computations with arrays easier. You can link to Javascript libraries for easier matrix operations - search web for Javascript matrix math library.

First, an example of a minimal .html text file to run Javascript in your web browser. All you need in the .html file is a script tag with your Javascript, using document.write() to write html to the web page or console.log() to write to the browser's Javascript console:

```          <script>
let a = 2;
console.log('minimal JS example, a = ' + a); // view in browser's Javascript console
let b = Math.pow(a,2); // raise a to power of 2
document.write('hello'); // display output on web page
document.write('<br> b = ' + b); // note <br> to start new line
</script>
```

 Javascript code MATLAB code // declare a variable then assign a value// numeric variables are double precision by default var a;a = 5.0; // or combine: var a = 5.0; % variables are not declared % numeric variables are double precision by default a = 5.0; // repeat which assigns values to// array elements// array indices start at 0  for (i=0; i<10; i++)  A[i] = i; % array indices start at 1% i = start:increment:end% where increment = 1 is default for i=1:10   A(i) = i;end

% use fprintf() function for % formatted output to command window for i = 0:2:10  fprintf(' %i \n', i)end // initialize 100x100 identity matrix var B = []; // declare 1D arrayvar N = 100;for(r=0; r" + D[r][0].toString();}// displays 1 and 4 D(1:2,1) % rows 1 to 2 of column 1% displays 1 and 4 // print all rows of column 1 of 2D array var old;for(r=0; r < D.length; r++) {  old = document.getElementById("output").innerHTML;  document.getElementById("output").innerHTML =    old + "
" + D[r][0].toString();}// displays 1, 4 and 7 D(:,1) % all rows of column 1% displays 1, 4 and 7 // logical expression var a = 1;var b = 2;if (a == 1 || b == 3) {  document.getElementById("output").innerHTML =    "a = 1 or b = 3";} a = 1;b = 2;if a == 1 || b == 3  fprintf('a = 2 or b = 3 \n')end // logical expresssion if (a == 1 && b != 3) {  document.getElementById("output").innerHTML =     "a=1 and b not 3";} if a == 1 && b ~= 3  fprintf('a=1 and b not 3 \n')end // if structure // note: use alert() here and below to save space vs.// setting innerHTML as done above if (a != 1) {  alert('a is not 1');} else if (b != 3) {  alert('b is not 3');} else {  alert('huh?');} if a ~= 1  fprintf('a is not 1 \n')elseif b ~= 3  fprintf('b is not 3 \n')else  fprintf('huh? \n')end // switch structure switch (a+b) {  case 1:    alert("a+b = 1");    break;  case 2:    alert("a+b = 2");    break;  case 3:    alert("a+b = 3");    break;  default:    alert("a+b > 3");} switch a+b  case 1    fprintf('a+b = 1 \n')  case 2    fprintf('a+b = 2 \n')  case 3    fprintf('a+b = 3 \n')  otherwise    fprintf('a+b > 3 \n')end // script which calls a function // variables defined outside a program unit // are globalvar y = 1;var z = 2;var b = myfunc(3); // call function myfuncalert("b = " + b.toString()); // function definition can be in// same file or separate file function myfunc(x) {  var a = x*y*z;  return a;} global y z % declare global vars before 1st usey = 1;z = 2;b = myfunc(3) % call function myfunc % function definition of myfunc% must be in a separate file named% myfunc.m (i.e., functionName.m) LISTING OF FILE myfunc.m  function a = myfunc(x)  global y z  a = x*y*z;  % returns last value of return variable in file // matrix multiplication// C = A*B// where A is I rows by J columns// B is J rows by K columns// and C is I rows by K columns // initialize matrix C so it can hold resultfor(i=0; i