Typewriter Text Effect JavaScript

Typewriter Text Effect JavaScript
Code Snippet:Typewriter in JavaScript
Author: timdotcode
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 4,659
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a typewriter text effect with a blinking cursor. It gets text strings from the array and applies a key hitting effect on each letter. You can also pass the text array through the data attribute on which you want to apply typing effect.

How to Create Typewriter Text Effect JavaScript

1. First of all, create a span element with the id “typewriter” and define its data-array attribute (or leave it if you want to define it in the JS program). Similarly, create a span element with a class name “cursor” that will show as a cursor. Wrap all these elements into a wrapper element.

<div class="wrapper">
   <div class="centeredBox"> 
     <span id="typewriter" data-array=""></span>
     <span class="cursor"></span>

2. After that, define the CSS style for the typewriter as follows:

/* Wrapper to Center the Typewriter */
.wrapper {
    background: #149cbe;
	color: #fff;
	font-family: "Roboto", sans-serif;
	font-size: 60px;
	width: 100%;
	height: 420px;
	display: flex;
	justify-content: center;
	align-items: center;

.centeredBox {
	width: 100%;
	text-align: center;

/* The Typewriter */


.cursor {
	border-left: 4px solid white;

3. Finally, add the following JavaScript code and done.

// The typewriter element
var typeWriterElement = document.getElementById('typewriter');

// The TextArray: 
var textArray = ["Hey, I'm Tim.","I like JavaScript.","I Love to Develop.", "I like this Typewriter."];

// You can also do this by transfering it through a data-attribute
// var textArray = typeWriterElement.getAttribute('data-array');

// function to generate the backspace effect 
function delWriter(text, i, cb) {
	if (i >= 0 ) {
		typeWriterElement.innerHTML = text.substring(0, i--);
		// generate a random Number to emulate backspace hitting.
 		var rndBack = 10 + Math.random() * 100;
		setTimeout(function() {
			delWriter(text, i, cb);
	} else if (typeof cb == 'function') {

// function to generate the keyhitting effect
function typeWriter(text, i, cb) {
	if ( i < text.length+1 ) {
		typeWriterElement.innerHTML = text.substring(0, i++);
		// generate a random Number to emulate Typing on the Keyboard.
		var rndTyping = 250 - Math.random() * 100;
		setTimeout( function () { 
			typeWriter(text, i++, cb)
	} else if (i === text.length+1) {
		setTimeout( function () {
			delWriter(text, i, cb)

// the main writer function
function StartWriter(i) {
	if (typeof textArray[i] == "undefined") {
		setTimeout( function () {
	} else if(i < textArray[i].length+1) {
		typeWriter(textArray[i], 0, function ()Â {
// wait one second then start the typewriter
setTimeout( function () {

That’s all! hopefully, you have successfully integrated this typewriter text effect code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

1 thought on “Typewriter Text Effect JavaScript”

  1. The code is really good. In the javascript attached, In the main typewriter function, in the callback function ‘A’ letter has to be removed for it to work properly


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link