Another one from the "this is not a syntax error" department.
The front-end developer is a strange beast who has to jiggle to and fro and code in several languages literally at the same time - javascript, html, css, php or some other server side language, some SQL dialect... No wonder that sometimes we make silly mistakes like:
var $myarray; var array = array(); $myarray = []; foreach(var i in myarray)
Last night I just did a silly mistake like this. In JavaScript I used the PHP way of concatenating strings. Something like:
var user = 'Stoyan'; alert('hello ' . user);
This is obviously wrong, but the thing is that it's not a syntax error as one might expect. It alerts "undefined". Why is that?
Well, 'hello' is a string object. You can call methods and properties on it, like:
>>> 'hello'.toUpperCase() "HELLO" >>> 'hello'.length 5
And spaces don't matter...
>>> 'hello' . length 5 >>> 'hello' . toUpperCase() "HELLO"
So 'hello' . user
is an attempt to access the "user" property of the string object 'hello'. This property doesn't exist, hence the "undefined" result.
Doing the opposite (using JavaScript-type concatenation in PHP) is also not an error:
$user = 'Stoyan'; echo 'Hello ' + $user; // prints 0
Comments? Find me on BlueSky, Mastodon, LinkedIn, Threads, Twitter