print a triangle in javascript code example

Example 1: how to draw a triangle using javascript

<!doctype html>
<html>
<head>
<body>
<canvas></canvas>
<script>
var canvas=document.querySelector('canvas');
var context=canvas.getContext(2d);
context.beginPath();
    context.moveTo(75,75);
    context.lineTo(10,75);
    context.lineTo(10,25);
    context.fill();
    </script>
    </body>
    </head>
    </html>

Example 2: print triangle in javascript

let n = 5; // you can take input from prompt or change the value
let string = "";
for (let i = 0; i < n; i++) {
  // printing star
  for (let k = 0; k < n - i; k++) {
    string += "*";
  }
  string += "\n";
}
console.log(string);