
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Randomize color by number in JavaScript
We are required to write a function that returns a random hex color. So here is the code for doing so −
Example
const generateRandomColor = () => { const keys = '0123456789ABCDEF'; let color = ''; while(color.length < 6){ const random = Math.floor(Math.random() * 16); color += keys[random]; }; return `#${color}`; }; console.log(generateRandomColor()); console.log(generateRandomColor()); console.log(generateRandomColor()); console.log(generateRandomColor());
Output
The output in the console will be −
#C83343 #D9AAF3 #9D55CC #28AE22
- Related Articles
- How to randomize (shuffle) a JavaScript array?
- Randomize string in C#
- How to sort data by text, date, number or color in Excel
- RGB color to hexadecimal color JavaScript
- Hexadecimal color to RGB color JavaScript
- Changing color randomly in JavaScript
- Random color generator in JavaScript
- Shuffle or Randomize a list in Java
- Generating random hex color in JavaScript
- How to choose a background color through a color picker in JavaScript?
- How to Randomize Lines in a File in Linux
- How to randomize an already created vector in R?
- How to randomize rows of a matrix in R?
- Number prime test in JavaScript by creating a custom function?
- Greatest number divisible by n within a bound in JavaScript

Advertisements