DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Unlocking Seamless Customer Relationship Management With React Integration Features
  • A Comprehensive Guide To Working With JSON in JavaScript
  • The Advantage of Using Cache to Decouple the Frontend Code
  • How I Used Swift Script in Electron Browser Natively

Trending

  • What Is CI/CD? Beginner’s Guide To Continuous Integration and Deployments
  • Testing Swing Application
  • Evolution of Software Architecture: From Monoliths to Microservices and Beyond
  • Building a Dynamic Chat Application: Setting Up ChatGPT in FastAPI and Displaying Conversations in ReactJS
  1. DZone
  2. Coding
  3. JavaScript
  4. TypeError: JavaScript

TypeError: JavaScript

If you're trying to learn to code in JavaScript, the vague TypeErrors can make you want to pull out your hair. But don't, we're here to help.

Saif Sadiq user avatar by
Saif Sadiq
·
Sep. 27, 18 · Tutorial
Like (9)
Save
Tweet
Share
27.2K Views

Join the DZone community and get the full member experience.

Join For Free

Can you add a number and a letter together?

If I ask you to give me the result of the addition of 1 plus H will you be able to give me the answer?

The obvious answer is NO.

The same goes in JavaScript! If you add 1 and H in JavaScript or when you try to perform operations on two operands of unmatched types, JavaScript throws a TypeError.

Per MDN: ‘A TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function.’

Therefore, it becomes necessary to make sure the variables must have the same data types before performing any operation. Type mismatch generates an error while executing the whole program.

Types of TypeErrors

For example, you will get Uncaught TypeError if you are trying to convert a number to an uppercase character. As toUpperCase() is a function to convert a string to uppercase characters, it will throw an error for following code structure.

Code structure


var num=1;
try{
num.toUpperCase();
} 
catch(err){     
document.getElementById("demo").innerHTML = err.name;
}

Error

How to Get Rid of This Uncaught Type Error: Cannot Set a Property

There are many methods possible to overcome this error.

1. Using the toString() Function

You can use the toString() function to convert a number into a string first and then you can convert that string to upper case characters using the toUpperCase() function.

var num = 1; 
try {       
num.toString().toUpperCase();   // Convert number into string first 
}
catch(err) {    
document.getElementById("demo").innerHTML = err.name; 
}

Output

"1"

2. Using Constructor New String() of a Predefined Class


var num = 1; 
num=new String(num);
try {   
num.toUpperCase();   // You cannot convert a number to upper case 
}
catch(err){     
document.getElementById("demo").innerHTML = err.name; 
}

Output


"1"

Here are few more TypeErrors that can be thrown by JavaScript in different browsers.

TypeError Related to console.log()

TypeError: Property 'log' of object # is not a function (Chrome)
TypeError: console.log is not a function (Firefox) 
TypeError: 'your string' is not a function (evaluating 'console.log("your string")') (Safari)
TypeError: Function expected (IE)

TypeError Related to prompt()


TypeError: Property 'prompt' of object [object Object] is not a function (Chrome) 
TypeError: prompt is not a function (Firefox) 
TypeError: 'a string, this could vary' is not a function (evaluating 'prompt("your question")') (Safari)
TypeError: Function expected (IE)

TypeError Related to confirm()


TypeError: Property 'confirm' of object [object Object] is not a function (Chrome)
TypeError: confirm is not a function (Firefox)
TypeError: 'a string, this could vary' is not a function (evaluating 'confirm("your question")') (Safari)
TypeError: Function expected (IE)
JavaScript

Published at DZone with permission of Saif Sadiq, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Unlocking Seamless Customer Relationship Management With React Integration Features
  • A Comprehensive Guide To Working With JSON in JavaScript
  • The Advantage of Using Cache to Decouple the Frontend Code
  • How I Used Swift Script in Electron Browser Natively

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: