Believe it or not, www. is a subdomain.
It is a widely accepted subdomain so people don't give it much thought, but it still is a subdomain.
In order for www. to work, you do need to add a DNS record for www.
This can either be an A record pointing to the ip address that is the same as domainnameexample.com (recommended) or a CNAME that points directly to domainnameexample.com.
Both A and CNAME will work, but an A record is recommended as it causes less overhead for the server as it might cause more queries overall.
If you use IPv6, make sure you also have an AAAA record unless you use CNAME, then the AAAA record of domainnameexample.com is automatically used if it exists.