diff --git a/files/en-us/web/javascript/a_re-introduction_to_javascript/index.html b/files/en-us/web/javascript/a_re-introduction_to_javascript/index.html index eea024dca7c6f1a..229e78ac3bda3fe 100644 --- a/files/en-us/web/javascript/a_re-introduction_to_javascript/index.html +++ b/files/en-us/web/javascript/a_re-introduction_to_javascript/index.html @@ -75,7 +75,7 @@
In practice, integer values are treated as 32-bit ints, and some implementations even store it that way until they are asked to perform an instruction that's valid on a Number but not on a 32-bit integer. This can be important for bit-wise operations.
-The standard arithmetic operators are supported, including addition, subtraction, modulus (or remainder) arithmetic, and so forth. There's also a built-in object that we did not mention earlier called {{jsxref("Math")}} that provides advanced mathematical functions and constants:
+The standard arithmetic operators are supported, including addition, subtraction, modulus (or remainder) arithmetic, and so forth. There's also a built-in object that we did not mention earlier called {{jsxref("Math")}} that provides advanced mathematical functions and constants:
Math.sin(3.5); var circumference = 2 * Math.PI * r; @@ -141,7 +141,7 @@Numbers
Strings
-Strings in JavaScript are sequences of Unicode characters. This should be welcome news to anyone who has had to deal with internationalization. More accurately, they are sequences of UTF-16 code units; each code unit is represented by a 16-bit number. Each Unicode character is represented by either 1 or 2 code units.
+Strings in JavaScript are sequences of Unicode characters. This should be welcome news to anyone who has had to deal with internationalization. More accurately, they are sequences of UTF-16 code units; each code unit is represented by a 16-bit number. Each Unicode character is represented by either 1 or 2 code units.
If you want to represent a single character, you just use a string consisting of that single character.
@@ -228,7 +228,7 @@Variables
Operators
-JavaScript's numeric operators are
++
,-
,*
,/
and%
which is the remainder operator (which is the same as modulo.) Values are assigned using=
, and there are also compound assignment statements such as+=
and-=
. These extend out tox = x operator y
.JavaScript's numeric operators are
+
,-
,*
,/
and%
which is the remainder operator (which is the same as modulo.) Values are assigned using=
, and there are also compound assignment statements such as+=
and-=
. These extend out tox = x operator y
.x += 5; x = x + 5; @@ -236,7 +236,7 @@Operators
You can use
-++
and--
to increment and decrement respectively. These can be used as a prefix or postfix operators.The
++
operator also does string concatenation:The
+
operator also does string concatenation:'hello' + ' world'; // "hello world"@@ -249,7 +249,7 @@Operators
Adding an empty string to something is a useful way of converting it to a string itself.
-Comparisons in JavaScript can be made using
+<
,>
,<=
and>=
. These work for both strings and numbers. Equality is a little less straightforward. The double-equals operator performs type coercion if you give it different types, with sometimes interesting results:Comparisons in JavaScript can be made using
<
,>
,<=
and>=
. These work for both strings and numbers. Equality is a little less straightforward. The double-equals operator performs type coercion if you give it different types, with sometimes interesting results:123 == '123'; // true 1 == true; // true @@ -263,7 +263,7 @@-Operators
There are also
-!=
and!==
operators.JavaScript also has bitwise operations. If you want to use them, they're there.
+JavaScript also has bitwise operations. If you want to use them, they're there.
Control structures
@@ -441,7 +441,7 @@Objects
obj[user] = prompt('what is its value?')These are also semantically equivalent. The second method has the advantage that the name of the property is provided as a string, which means it can be calculated at run-time. However, using this method prevents some JavaScript engine and minifier optimizations being applied. It can also be used to set and get properties with names that are reserved words:
+These are also semantically equivalent. The second method has the advantage that the name of the property is provided as a string, which means it can be calculated at run-time. However, using this method prevents some JavaScript engine and minifier optimizations being applied. It can also be used to set and get properties with names that are reserved words:
obj.for = 'Simon'; // Syntax error, because 'for' is a reserved word obj['for'] = 'Simon'; // works fine @@ -451,7 +451,7 @@Objects
Starting in ECMAScript 5, reserved words may be used as object property names "in the buff". This means that they don't need to be "clothed" in quotes when defining object literals. See the ES5 Spec.
-For more on objects and prototypes see Object.prototype. For an explanation of object prototypes and the object prototype chains see Inheritance and the prototype chain.
+For more on objects and prototypes see Object.prototype. For an explanation of object prototypes and the object prototype chains see Inheritance and the prototype chain.
Starting in ECMAScript 2015, object keys can be defined by the variable using bracket notation upon being created.
@@ -663,7 +663,7 @@{[phoneType]: 12345}
is possible instead of justvar userPhone = {}; userPhone[phoneType] = 12345
.Functions
The second argument to
apply()
is the array to use as arguments; the first will be discussed later on. This emphasizes the fact that functions are objects too.-@@ -727,7 +727,7 @@You can achieve the same result using the spread operator in the function call.
+You can achieve the same result using the spread operator in the function call.
For instance:
avg(...numbers)
Functions
Custom objects
-For a more detailed discussion of object-oriented programming in JavaScript, see Introduction to Object-Oriented JavaScript.+For a more detailed discussion of object-oriented programming in JavaScript, see Introduction to Object-Oriented JavaScript.In classic Object Oriented Programming, objects are collections of data and methods that operate on that data. JavaScript is a prototype-based language that contains no class statement, as you'd find in C++ or Java (this is sometimes confusing for programmers accustomed to languages with a class statement). Instead, JavaScript uses functions as classes. Let's consider a person object with first and last name fields. There are two ways in which the name might be displayed: as "first last" or as "last, first". Using the functions and objects that we've discussed previously, we could display the data like this:
@@ -769,7 +769,7 @@Custom objects
s.fullNameReversed(); // "Willison, Simon" -Note on the
+this
keyword. Used inside a function,this
refers to the current object. What that actually means is specified by the way in which you called that function. If you called it using dot notation or bracket notation on an object, that object becomesthis
. If dot notation wasn't used for the call,this
refers to the global object.Note on the
this
keyword. Used inside a function,this
refers to the current object. What that actually means is specified by the way in which you called that function. If you called it using dot notation or bracket notation on an object, that object becomesthis
. If dot notation wasn't used for the call,this
refers to the global object.Note that
this
is a frequent cause of mistakes. For example: