Create a String of Variable Length in JavaScript-Code Examples

Radu TM

Create a String of Variable Length in JavaScript

JavaScript by Radu TM June 17, 2022

//Create a String of Variable Length in JavaScript
//This function will create a string of variable length
//The string will be made up of random characters

function createString(length) {
  //Create an empty string
  var str = "";
  //Loop through the length argument
  for (var i = 0; i < length; i++) {
    //Add a random character to the string
    str += String.fromCharCode(Math.floor(Math.random() * 256));
  }
  //Return the string
  return str;
}

0

21.390

Create a String of Variable Length in JavaScript

JavaScript by Radu TM June 17, 2022

// create an empty string variable
var str = "";

// use a for loop to add characters to the string variable
for (var i=0; i < 10; i++) {
  str += "x";
}

// print the string variable
console.log(str);

0

21.390

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories