The examples below make use of the log function of the console object present in most browsers for standard text output.
Brendan Eich summarized the ancestry of the syntax in the first paragraph of the JavaScript 1.1 specification[1][2] as follows: JavaScript borrows most of its syntax from Java, but also inherits from Awk and Perl, with some indirect influence from Self in its object prototype system.JavaScript is case sensitive.
[4] A variable's identifier must start with a letter, underscore (_), or dollar sign ($), while subsequent characters can also be digits (0-9).
This is different from a function expression being assigned to a variable in a var, let, or const statement.
Note that JavaScript's strict mode forbids the assignment of an undeclared variable, which avoids global namespace pollution.
The value of "undefined" is assigned to all uninitialized variables, and is also returned when checking for object properties that do not exist.
Unless explicitly converted, the undefined value may behave unexpectedly in comparison to other types that evaluate to false in a logical context.
Although this format provides an accuracy of nearly 16 significant digits, it cannot always exactly represent real numbers, including fractions.
For example: As a result, a routine such as the toFixed() method should be used to round numbers whenever they are formatted for output.
In JavaScript, strings can be created directly (as literals) by placing the series of characters between double (") or single (') quotes.
When used in a logical context, 0, -0, null, NaN, undefined, and the empty string ("") evaluate as false due to automatic type conversion.
All other values (the complement of the previous list) evaluate as true, including the strings "0", "false" and any object.
When type conversion is required, JavaScript converts Boolean, Number, String, or Object operands as follows:[10]
Douglas Crockford advocates the terms "truthy" and "falsy" to describe how values of various types behave when evaluated in a logical context, especially in regard to edge cases.
Automatic type coercion by the comparison operators may differ for cases of mixed Boolean and number-compatible operands (including strings that can be evaluated as a number, or objects that can be evaluated as such a string), because the Boolean operand will be compared as a numeric value.
An expression can be explicitly cast to a Boolean primitive by doubling the logical negation operator: (!!
Arrays, unlike the basic Object type, are prototyped with methods and properties to aid the programmer in routine tasks (for example, join, slice, and push).
A Date object stores a signed millisecond count with zero representing 1970-01-01 00:00:00 UT and a range of ±108 days.
The ECMAScript 2015 standard introduced the "..." array operator, for the related concepts of "spread syntax"[12] and "rest parameters".
Unlike arguments, however, rest parameters are true Array objects, so methods such as .slice() and .sort() can be used on them directly.
In early versions of JavaScript and JScript, the binary logical operators returned a Boolean value (like most C-derived programming languages).
The await operator in JavaScript can only be used from inside an async function or at the top level of a module.
Objects are entities that have an identity (they are only equal to themselves) and that map property names to values ("slots" in prototype-based programming terminology).
For example, in a browser, typical host objects belong to the DOM (window, form, links, etc.).
It is a convention that constructor functions are given a name that starts with a capital letter: Object literals and array literals allow one to easily create flexible data structures: This is the basis for JSON, which is a simple notation that uses JavaScript-like syntax for data exchange.
When called as a method, the standard local variable this is just automatically set to the object instance to the left of the ".".
So, Functions are objects themselves, which can be used to produce an effect similar to "static properties" (using C++/Java terminology) as shown below.
The Mozilla implementation allows for multiple catch statements, as an extension to the ECMAScript standard.
They follow a syntax similar to that used in Java: In a browser, the onerror event is more commonly used to trap exceptions.
However, eval represents a major security risk, as it allows a bad actor to execute arbitrary code, so its use is discouraged.