Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I cannot count the words in Javascript. Instead of counting words, it gave me an error, like this.
Uncaught TypeError: Cannot read property 'split' of undefined


What I have tried:

I tried to use the split() method but it does not work and gave me an error
Posted
Updated 3-May-21 4:30am
Comments
Chris Copeland 30-Apr-21 4:32am    
You haven't provided us with any examples or code so we can't diagnose the issue. We need to be able to see what you've written in order to be able to provide help.
Richard MacCutchan 30-Apr-21 6:52am    
Look at the error message. it is telling you that you are trying to call split on a null object. So you need to examine your code and find out why the object reference at that line is null.

1 solution

You haven’t supplied us with any code for your problem.

I assume you want to count the number of words in a string

You can use the ".length" property of arrays to find how big it is.



var myString = "How are you doing today?";
var words = myString.split(" ");

console.log(words.length)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900