6

As I learn JavaScript I've been looking around the web and seen numerous references to constructs in Javascript, but I can't seem to find a complete definition of what they are and what they are not, especially in the context of Javascript.

For instance, in 'Similar Questions' I see links that lead to an example featuring the following code:

In What is this construct in javascript?:

(function () {

})();

From what I understand this is a construct, but what are they defined by?

Community
  • 1
  • 1
fakeguybrushthreepwood
  • 2,983
  • 7
  • 37
  • 53

3 Answers3

13

Construct is a generic term referring to an arbitrary aggregate of code in a specific formation. It is not a javascript-specific term.

Basically, it can apply to anything. So, while the code you referenced is a construct known as a self invoking anonymous function, var x = "hello world"; is a construct known as a variable declaration and assignment.

smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • In JavaScript, could it be said that anything that ends in a ; is a construct? Up until the ; ? – fakeguybrushthreepwood Sep 03 '12 at 05:47
  • Anything is a construct. A keyword is a construct. A value literal is a construct. The semicolon is not necessary. You are overthinking this. – smartcaveman Sep 03 '12 at 05:50
  • But if var x = "hello world"; is a construct, based on what you said that 'Anything is a construct' does it mean that 'x' or just 'world' is a construct? Doesn't it refer to a line of code somehow? I know this probably sounds ridiculous - but I really am not 100% clear here. – fakeguybrushthreepwood Sep 03 '12 at 05:59
  • Yes. x is a variable. The "world" is a string literal. Variables and string literals are both constructs in javascript. The definition of construct in terms of javascript is the same definition you will find in a standard english dictionary – smartcaveman Sep 03 '12 at 06:13
  • "Statement" is the term for the language constructs that end with `;`. – Ben J Mar 01 '16 at 19:02
5

A "language construct" is the full term you're looking for. According to the linked definition:

A language construct is a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of a programming language.

So it's any valid segment of written code that follows the rules of the language. It's a generalisation of words like "expression", "statement", "function argument list", "assignment statement", "keyword", "function definition", etc. that each define a series of tokens to look for and what they will mean in the rules of the language. The code of a completed program is constructed with them.

Ben J
  • 589
  • 8
  • 12
  • "It's a generalisation of words like..." is why this is the winner for me. Makes it clear that "construct" is a meta concept, not a coding pattern. – Tom Jan 19 '17 at 19:33
0

From the Scripting Reference in MASTERING HTML4 by Deborah and Eric Ray:

Constructs are the structures that you can use in a JavaScript to control the flow of the script

They go on as to alphabetically list ALL such a controls which include Break, Comment, If, If else.... So a construct is a very specific term definition in JavaScript used to name and encompass all the statements (structures) that control the flow of a script.

Web_Designer
  • 72,308
  • 93
  • 206
  • 262
cpera
  • 11