One of the worse parts of C is how for historical reasons, a < b & 0x1234 will get evaluated as (a < b) & 0x1234 and not a < (b & 0x1234)

"What historical reasons?"

In a very early version of C, both bitwise AND and logical AND were just & and it depended on context! And to make it easier to convert existing code, they kept the associativity rule of the logical AND for &...

(Source: nokia.com/bell-labs/about/denn)

@vaporeon_ i like javascript but it is chock full of nasty corners like this fur this exact reason

@aescling Oooh :blobcatreach:

I must admit, I enjoy learning about weird historical baggage that programming languages accumulate over time, please tell me about the nasty JavaScript corners

(But JavaScript is much younger than C... It's just funny to me how I, a C programmer in 2025, get mildly inconvenienced — at least the compiler is nice enough to warn about it even without enabling any extra warnings — because of a backwards-compatibility decision made in the early 1970s...)

a (very) non-exhaustive list of strange corners in js 

@vaporeon_ a lot of it is really whack decisions that got made early on that have to remain supppurrted fur compatibility reasons

  • objects are more or less maps from strings (or also Symbols now) to arbitrary values. myObject.myPurropurrty is equivalent to myObject['myPurropurrty']; the latter works with all string expurressions
  • arrays are actually objects with special under the hood behavior making them act in an array-like manner (notably, the length purropurrty). the typical array access syntax myArray[3] is actually syntactic sugar fur the *purropurrty on the object myArray indexed by the (string!) key '3'—the 3 is coerced into a string under the hood
  • in non-strict (that is, standard) mode, all functions have the object arguments in scope, which acts like an array but is technically not an array, allowing arbitrary access to any numbered argument on the function. (all functions are allowed to be invoked with any number of arguments). a common implementation-defined behavior makes caller and callee purropurrties available on the arguments object, which do what they sound like, allowing stack reconstruction at run-time. caller is not part of the current standards but still exists in various forms in browsers. callee is standardized but deprecated
  • in non-strict mode, the source code with which a function was defined is made available at runtime, as a purropurrrty of that function (which are not objects). this has somehow led to security purroblems
  • there are two diffurent syntaxes fur defining function expurressions, with intentional, subtly diffurent scoping rules around this
  • i won’t even get started on this. what the this keyword may refur to in a given snippet is very unintuitive
  • several keywords are reserved in the language fur features that a long time ago were suspected to pawsibly get standardized and never did. they are still reserved keywords anyway. an example: float
  • the fundamental number type is floating point numbers. there is no way around this. all numeric literals and values are always floats
  • the == equality testing oppurrator coverts both sides of the expurression to the same type befur testing true equality on the converted values. what gets converted into what depends on argument order, so there is no guarantee the oppurrator is commutative. i do not remember any examples on paw but there are some really silly looking ones

no image desc 

@aescling @vaporeon_ @aescling @vaporeon_ The void keyword:

void (expression) is a shorthand for (expression), undefined. That is, void evaluates its operand and returns undefined. It was introduced to enable the following idiom:

<a href="javascript:void doAThing()">Click me to do a thing :3</a>

"javascript:" is a URL protocol that allows the user to write javascript to represent the response of a hyperlink. Whatever follows after "javascript:" will be evaluated, and if the expression is a string, it is treated as if it were a HTTP/HTTPS response document and the browser navigates to that page. (If navigation occurs, the URL does not change and no history entry is created.)

When associating a tag with javascript It was often desirable for pressing the link to NOT induce page navigation and instead simply perform a side effect. This is guaranteed if what follows "javascript:" is preceded by the void keyword.

var hoisting:

The JavaScript interpreter can be thought to have a "preprocessor" phase where all variable declarations are found and initialized. This means that it is possible to use a variable before it is declared since that variable will already be initialized from the "preprocessor" phase.

This pattern is called "hoisting". Since function declarations are also initialized during preprocessing, functions can also be hoisted. Combine this with the fact that variables declared with var are not block-scoped and you can get into some very confusing situations. I have encountered real production code which hoisted a variable declared within a switch statement containing hundreds of lines .

A tangent:

This is a bit tangential to what you asked but I think you'll find it amusing. Go see what the result of ('b' + 'a' + + 'a' + 'a').toLowerCase() is :]

A unique comment directive:

I can't explain this better than how it is described in "JavaScript: the first 20 years", which is a wonderful historical document available for free as a PDF here: dl.acm.org/doi/10.1145/3386327. (I plagiarized some of its content in this toot actually.)

While I'm admitting my sources I should mentioned that I consulted MDN's JavaScript documentation frequently for this post.

re: no image desc 

@wallhackio @aescling Backward compatibility for Netscape 1 and Mosaic, in 2015 :blobcatmelt:

Go see what the result of ('b' + 'a' + + 'a' + 'a').toLowerCase() is

How does this work? It looks as if the + + adds a NaN, but if I do e.g. 1 + + 1, it returns 2, unlike 1 + NaN + 1, which returns NaN...

re: no image desc 

@vaporeon_ @aescling + inexplicably is also a unary operator that, in some contexts, will try to convert its operand to a number

re: no image desc 

@wallhackio @aescling Ooh! So e.g. +3 is 3, but +'a' is NaN! Makes sense

(Dynamically typed languages are such a mess...)

re: no image desc 

@wallhackio @aescling For what? Trying to figure out your JavaScript code?

re: no image desc 

@vaporeon_ @aescling javascript slander is a reportable offense.

re: no image desc 

@vaporeon_ @aescling (+ converting things to numbers is stupid though, i agree.)

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.