If exercises could have richer content then book is better. Gabe rated it it was amazing Aug 15, James W Selway rated it it was amazing Jul 07, Wayne rated it it was amazing Oct 25, William Diaz rated it really liked it Dec 27, Sarath rated it it was amazing Dec 10, Chris Mcmanaman rated it it was ok Oct 08, Steven Murawski added it Sep 22, Ken Weber marked it as to-read Sep 26, Sean Smyth is currently reading it Oct 22, Ivan Villamizar is currently reading it Jan 01, Bradford added it Jan 12, Michael Wakefield is currently reading it Mar 14, Cloud Monk Losang Jinpa added it Apr 30, Zhaoxian Hu added it Jun 02, Tavi is currently reading it Jun 03, Jarek Kicior is currently reading it Jun 25, Bret Jordan is currently reading it Mar 12, Charlie Sparks is currently reading it Mar 12, Patrick is currently reading it May 28, Wali marked it as to-read Feb 27, There are no discussion topics on this book yet.
Be the first to start one ». Goodreads is hiring! If you like books and love to build cool products, we may be looking for you. Learn more ». About Tony de Araujo. Tony de Araujo. Books by Tony de Araujo. Need another excuse to treat yourself to a new book this week? We've got you covered with the buzziest new releases of the day.
To create our Read more Trivia About TypeScript in Pla No trivia or quizzes yet. Console and Arcade video game emulator for macOS. OpenEmu is about to change the world of video game emulation. Download Now v2. Where all it started, and still going strong after 20 years as one of the classic party fun games. To be honest, this wasn't my favoriite Mario Party game, but it's still fun.
Playing this game alone won't be playing this game at it's best. It's really the multiplayer where it's at. There is a ton of minigames you will have a blast playing with your friends. So be sure to try out this game. I was turned on by Mario Party simply because of the name - because it just sounded so cool. I really didn't know how Mario could be a 'party', but this game was really neat!
It was very unique too at the time of it's original release. The variables on line 2 were assigned to the object properties based on matching names. If you reverse the names, you still get the correct information in each one of them. If you use other names, such as for example, xyz, you will get an error. Also, notice the curly braces. The braces need to match the data structure you are attempting to de-structure.
When it comes to de-structuring arrays, the sequence of presenting your variables is important because the values will be assigned from left to right. In objects, the variable declaration sequence does not matter. What matters is the name given to each object property, which should match the property name of the data source we are binding to the variable.
Next, let's look at another important technique: spreading items from an array. The Spread Operator The spread operator is represented by the three-dot ellipsis In JavaScript ES5 it would take several steps to do the operation, but in Typescript all we have to do is to use the spread operator to break the source array into its individual components. Although we did not include a string value as an argument for push , it will work because the TypeScript interpreter will evaluate the array and submit each individual string item to push.
That saves us a lot of work. Spread operators can be used whenever we need to submit the contents of a complex data structure as individual data components. A function is used to store code that performs a specific task. This task might consist of many lines of code stored in the functions body. We can later invoke the function every time we need to run the code. You can immediately see the benefit of functions since it only takes one line to invoke the function.
In other words, functions are "self-contained" modules of code that accomplish a specific task. Speaking of modules, later we will also learn how to create modules that may contain several related functions. Sometimes functions accept external data to be internally processed, and then they "return" an output result. Other times a function has a specific job to do and no external data is necessary. Once a function is written into code, it can be used repeatedly we instantiate the function by merely invoking the function.
Functions can also be invoked from inside of other functions. Here is an example of a simple function. This function does not take any data input. Its purpose is to accomplish a task whenever we invoke the function:. If you write this function on the TypeScript playground and run the program, nothing will happen.
Calling a function or invoking a function means the same thing. To call a function we just write the name of the function and add a pair of parentheses as a suffix one word :.
Now, when you run the program, you will see the "Hello world! If your intention is to feed data into the function so that it gets processed by the function, you need to add one or more input parameters inside of the parentheses of our function signature the top line.
There are other ways to do this, as we will see later:. Make sure "Tony" is wrapped in quotes because it is a string value. If you don't wrap the input argument in quotes, the interpreter will assume that it is a variable, and it will look for such variable in your code.
As discussed previously, numbers will be passed into the function without being wrapped in quotes. This works because numbers cannot be the name of variables. Another thing to keep in mind as far as terminology is concerned, is that an argument is what we call the data being passed into a parameter. In other words, functions have input parameters and parameters accept arguments. Making sure the function input data type is validated What happens when I call the function and pass in the number 3?
We can actually write a list of rules for our code and store it in a data type called interface. Interfaces will be covered later in the book. Right now, let's just stick to functions since we are building our TypeScript awareness from the ground up. In order to ensure that our functions input is always of type string if that is our intention , we can program the function signature this way:.
Now, if we ever call the function and pass in a number, TypeScript complains because it is the wrong type of argument. The return value Each function needs to return a value.
In our previous example we did not return anything, we only displayed a message on the screen monitor. Functions have a tremendous advantage over code written in the global environment see my book JavaScript Objects Functions and Arrays Explained [20] for a greater detail on that topic. The return mechanism is unique to functions since we cannot use it anywhere else but in a function.
The purpose of the return statement correct terminology is twofold: 1- To export a value from within the function in its raw format. When the function call ends its process and it always ends by the execution of a return which acts as a break , the execution context is cleared from working memory. Since having a return is very important to JavaScript in order to wipe the function execution from working memory, every JavaScript function invocation returns automatically if there is no explicit return stated.
The automatic return generated by the JavaScript interpreter is always the value of undefined, which is of type undefined. That is what happened in our little greeter example, an automatic return was generated without being noticed by us.
Now, TypeScript was created to prevent ambiguity when it comes to writing JavaScript code. The TypeScript type checking helps us become more conscious and better programmers.
In TypeScript, we can specify what kind of return data type we intend to use in the function, including the usage of void, which means that no return is intended from the programmers part and therefore JavaScript should do its return thing automatically. Using an explicit return type declaration helps you and other programmers to inspect, or to create, your lines of code project.
Remember, we only want to explicitly use return, if we need to export the raw value from the function. Whenever we display data on the screen by using alert, console. On the other hand, when we need the raw data for further processing, then we have to return it, which is a way of exporting the data out of the function.
Note: We can also assign the data to an external variable in order to export it but right now, we are talking about the return mechanism.
Let's see how we can use the return value by modifying our example. If, on my example, I replace the "void return type declaration", with a string return type, I immediately get a warning from the TypeScript interpreter, stating that it is now expecting an explicit return statement of type string inside of the function body, and there is no such implementation written, as you can see below:.
The catching is done by assigning the returned value to an external variable when I call the function see the second line from the bottom on this example :. Of course, the external variable is optional and TypeScript will not care whether we catch the value or not. TypeScript only wants to make sure that my function signature is consistent with what I program in the body of the function. By the way, when we declare the external variable result , we may also add a type classification to it.
Adding types is a good practice because it triggers the TypeScript editors error correction when we attempt to call the function lines later. We could declare variable result like this:. This way, the TypeScript interpreter and any human programmer inspecting the code later will know that the function is supposed to return a string value. Setting default parameter values Let's look at another example. This one will have multiple input parameters:.
In the above script, we see a function that takes two arguments via its parameters and then displays the result of their sum. These two parameters take arguments of type number.
If we attempt to pass in any other type of arguments when we call the function, the TypeScript interpreter red flags it. When I call the function addMe and pass in numbers 5 and 6, it results in a display of 11, and that is fine.
The TypeScript interpreter will flag my function invocation because one of the parameters is missing an argument. We feed arguments to parameters. In order to prevent errors of omission when we invoke functions, TypeScript introduces the ability to set default values to the input parameters of a function. This can be helpful in case we miss one of the input arguments when we call the function, which normally results in error for TypeScript, or undefined for JavaScript.
Besides error prevention, this feature also expands the capability of function design by making it easier to write certain odd implementations. To provide a default value to parameters, we do it in the following manner:. If you are trying this example along with me, notice how much more work we would need to do to accomplish this in common JavaScript:. Please note: When mixing default parameters with regular parameters, make sure your default parameters are programmed to the right of the regular parameters because, when we invoke the function, the arguments are applied sequentially to the parameters.
Optional Parameters Besides introducing default parameters, TypeScript introduces optional parameters. Whenever we apply an argument to an optional parameter, it triggers a different part of the script. Normally we get an error, when we write a parameter in JavaScript and then call the function but do not pass in an argument to the parameter. To prevent this problem, TypeScript allows for the creation of optional parameters.
An optional parameter is followed by a question mark?. Like in default parameters, just make sure your optional parameters are the last parameters to the right, otherwise the option feature becomes useless when you call the function because arguments are always matched to its parameters in a left to right sequence.
Now, in this example, when we can call the function with just two arguments, it triggers the else portion of the conditional statement:. This is a Boolean question and the answer is either true or false. In other words, does input3 have a value other than false, zero, null or undefined? Otherwise, the else code block is executed. The rest parameter syntax allows a function to represent an indefinite number of arguments as an array of arguments.
To remember the name rest just think it this way, here is your first parameter, here is your second parameter, and here are the rest of the others. The rest parameter needs to be the last option in the parameter signature layout or the only one.
To indicate that a parameter is a rest parameter we prefix it with three dots. Here is an example where c is a rest parameter:. Parameters a and b are regular parameters. Then, parameter c is an array of an unknown number of arguments. Remember, arguments are the data being passed to parameters. Parameters are the variables accepting arguments. Arguments 2 and 5 are assigned to parameter a and b. The rest of the numbers are assigned to an array mapped to parameter c.
Additionally, we can have just one parameter and make it a rest parameter because it is also the last one:. Since the rest parameter set is a real array, we can actually take advantage of all the methods available to array objects see my eBook [20] on array methods. Side note: lines 3 and 4 are just two different ways of logging similar expressions.
This function places all the arguments passed into the parameter into an array. Then, it calculates the sum of all its arguments. You can pass as many arguments into the function as you want, and the loop will make sure all arguments are processed. I can now assign a function call to a variable and then pass in a bunch of numeric arguments.
Then, in my example, when I alert the result it displays The variable total was declared inside of the function. This variable is not seen from the outside.
When the function returns, the value of the variable is reset. This is another advantage of functions because we can privatize variables, which remain in separate contexts as we call the same function.
After the function execution, these variables are cleared from memory. I will cover private variable in more detail later on. At the end, the function returns the final value of variable total and resets total back to zero. If you happen to catch the returned value from the outside, you can reuse it. If you don't catch the return value, it will be lost in forever. Questions with further explanation:.
A rest parameter must be of an array type. This is done automatically. If we try to change the data type of a rest parameter like in the following example:. On our previous script, function addTotal does not specify what kind of arguments it accepts and this could lead into an implementation error. For example, we could pass in "blue", "red", "green" and the function would gladly process it like "blueredgreen". Since the purpose of the function is to add numbers, we should make sure that the rest parameter only accepts numbers.
The way to do it is by adding the data type to the parameter, not as a number, but as a numeric array:. From this point on, only numbers will be accepted when we call function addTotal.
Please read on because the new terms used here will be explained in more detail as we move along. An arrow function expression is a shortcut for a function expression which is an anonymous function assigned to a variable. See example below. An arrow function expression in TypeScript is not bound to the local this, which is a pointer to the parent object. In fact, an arrow function in TypeScript does not have a this of its own at all as we will see later and we need to be careful when implementing a this in arrow functions since TypeScript will point it to an execution context that may be different than what we intend it to be.
We call it "anonymous" because it has no name of its own. It is often assigned to a variable, which acts as the function's name. This is just another way of declaring functions.
Both methods, this one and the previously discussed method, are common. Programmers use one method or the other. The reason to use anonymous functions is by preference or by necessity. The necessity part comes from the fact that anonymous functions have great flexibility - they can be embedded into existing code.
The arrow function expression is something new to JavaScript. It is a shortcut to represent an anonymous function. Aside from being a shortcut, an arrow function offers some advantages and some pitfalls as well. To write the above function as an arrow function we would do it like this:. If you are trying these examples in the playground, watch how the interpreter converts the arrow function back to a regular function expression when transpiling to JavaScript. This is because arrow functions are not yet accepted in most browsers.
However, read on because there are some useful implementations when using arrow functions in TypeScript. The function keyword is missing, the explicit return statement is missing and so are the curly braces.
If you add an explicit return statement or any other executable code, you will need to add the curly braces back. You may also see arrow function shortcuts looking like the one in the following example no parentheses for the parameter :. This is common when there is only one parameter x is the parameter in our example. When we enter more parameters, we need to put the parentheses back.
On the other hand, if there are no parameters in the function, you may find the following shortcut:. Arrow functions are also known as lambda expressions [26]. About the THIS in arrow functions. In general, the "this" keyword is a placeholder for the object that owns the expression.
However, there are exceptions based on the context at the time of calling. Contrary to a regular function expression, in an arrow function there is no "this" built in.
Consider the following script to see how there is no "this" in a TypeScript arrow function:. When we invoke function myFunc we get object Window as the displayed result and this is correct since object Window owns this function. We need to know when an arrow function is helpful, as well as when it may get us in trouble.
Consider the following object containing a regular arrow function inside it: Please test it on your playground. Which x do you think will show on the screen? Try it on the TypeScript playground [3]. That is because the program searches for variable x from inside out, and it uses the first variable x that finds.
This is correct. What if, we actually wanted the arrow function to access the property x of object outerObject, instead of its own x? The normal way to access the property x from object outerObject is by writing it as alert this. Then normally , the "this" is replaced by the object name at runtime and the value "Im the object x" is displayed.
However, TypeScript arrow functions will fail when we implement them this way because they do not recognize the keyword "this" and do something else instead. Before we modify our original TypeScript code from the above example to include a this in its arrow function, please look at the current JavaScript version in your playground.
It should look like the one in the following image:. Fig 8 This is the JavaScript version of our first example. TypeScript replaced the arrow function for a standard function expression because browsers do not yet support arrow functions. TypeScript replaced the arrow function with a regular JavaScript function expression. The output result for the alert method is the value of local variable x.
As asked before, what if we wanted to access the objects x value, instead of accessing the functions x value? Normally in JavaScript, if we intend to access the objects x property instead of the inner x variable, we need to use this. Here is what I mean:. However, the above script will not work. Notice what TypeScript does to its JavaScript version as soon as we add this.
It would have worked fine if our intention was to grab the global x, but the x we want is the one inside of the object and therefore the result is incorrect. TypeScript assumes that the programmer wants to access the global environment. One thing we can do is to avoid using the arrow function in cases where we are going to use the keyword "this" to access the current object.
If our intention is to grab the objects local property via one of its methods, a regular anonymous function is a better choice than an arrow function. Another thing we could do is to hardwire the objects name to variable x in the alert method: alert outerObject. Use a regular function expression if you want to access a local "this" in a single object literal. However, when it comes to classes instead of single objects, TypeScript acts differently.
Therefore, what we may perceive as a flaw in TypeScript is actually made by design to solve certain advanced problems. We will get there soon. Here's an example of using a regular function expression when we want to access the parent object:. Please note: Arrow functions with a modified "this" have certain benefits that need to be explored. They work this way for a reason and we will be able to see a few examples ahead, but first, lets make sure we master the concepts we have just covered by exploring the subject a bit more.
The following function includes an inner function that adds words or sets of words to build a complete string. This inner function is an arrow function. The variable seen on line 1 is just for testing purposes and we will use it later. Please examine the code before continuing. I will explain it below the image but it is best if you take a hard look at it before reading my explanation. On line 4 there is an empty string variable.
This variable will accumulate words to build a complete string at the end. This variable has the same name as the global variable on line 1, which is not a good practice. I have done this on purpose in order to test accessibility to it later in the exercise.
The reason for including the variable word line 4 inside of function stringBuilder is to keep it private so that no one can edit it globally. Another reason could be to avoid possible conflicts with global variables. A third reason could be to improve memory management.
The function stringBuilder returns another function on line 5, which I have named newWord. The reason for using an inner function is so that, every time we call the function to add new data to variable word, the variable word holds this data and it does not reset its value.
In other words, if we only used one function, variable word will reset each time we called the function. The inner function allows for permanent storage as described next. In order to access the inner function newWord , function stringBuilder is assigned to variable addWord on line This assignment is actually a function invocation and therefore, the value returned to variable addWord is the inner function in its explicit form. This means that the private variable word inside of the outer function will remain in memory for the life of the program, because the inner function assigned to addWord has closure on the private variable word.
To have closure on a variable is synonymous with keeping that variable in its scope until it is no longer needed even after the outer function ends its process. On lines 12, 13, 14 and 15, the function is invoked and several words are passed in. The inner function adds these words to variable word, which concatenates them into a whole string paragraph. The top variable assigned to groundHog has nothing to do with this script but it will be important on our next example.
Test the script on the TypeScript playground. It should work fine since we do not have any issues yet. Take a few minutes to study the code and see why and how it works. Notice how each time the alert fires up, it adds a new word to the output. Next, let's do an experiment: what would happen if instead of an outer function, we had an object, and the inner function was a member of the object? Lets look at that possibility next.
The stringBuilder is now an object instead of a function. It has an internal property called word on line 4, and on line 5, it has a method to build our string. This method happens to be an arrow function, and because we want to access the objects property word, we are using the this keyword as a placeholder for the objects name and this is wrong as you will see next. When we run this script, the JavaScript interpreter uses the external variable word and the first term that comes up in the output string is groundHog.
Then, TypeScript assigns it to the global this outside of the object and that creates an error because we really want to address the property word, not the global variable word.
What can we do? Do not use an arrow function in single objects if you are going to use this to address a local property. However, if you instantiate the object from a class, it will work. We have not yet covered classes so, if the next sample script looks confusing, please move on and return later after we cover classes. This next topic is included here for future reference when you review the book. In this case, the object is instantiated from a class.
Classes are cover on a later chapter. For now we are just concerned with implementation of "this" in arrow functions. The method newWord lines 4 through 6 successfully addresses the correct property word line 3 when called from the instantiated object stringBuilder which are lines 8, and then 12,13,14, This is because TypeScript redirects the arrow functions "this" to the class "this", as opposed to when an object is created without using a class.
Just out of curiosity, how would we go about addressing the outer variable word line 1 from within the newWord method, when implementing a class? To address the global variable word, all we have to do is to remove the "this" from the returned word value line 5 on the TypeScript fig Another way, if you want to be more specific, is to declare another variable at the environment you want to target after line 1, fig13 and assign it to its local "this", then point the methods word to it currently on line 5.
You can of course, use another name for the variable. Besides "that", "self" is also a popular name for this kind of application. Whatever name you choose, keep it consistent on your programs. On the next topic, we will see how TypeScript successfully uses arrow functions to solve problems normally found on common JavaScript code. For now, please look at the following example code.
You may also skip this topic and return to it after we cover classes. On line 1, there is a global variable with a value of This variable serves as a test against the class property of the same name test , which is found on line 3. The inner function on line 5 will select either the global variable test, or the class property test, via its this pointer. Lines 4 through 8 comprise of a method myFunction which invokes a window method setTimeout , which in turn triggers an inner function that alerts the value of test.
That is because the method setTimeout is an inner function inside of a method. Inner functions inside of methods are not methods, they are independent functions and they belong to the global context. To make things worse, setTimeout is a method from the window object and therefore it is global by nature, but even a simple inner function would have its this pointing at the global object.
If setTimeout confuses you, heres an example of the same script using just a simple inner function: [34] icontemp. This script has already been explained below the image before last.
In this current image, we now have an arrow function on line 5 instead of a regular anonymous function. The arrow function will direct the this to access 55 instead of Below, please find the JavaScript result after being transcompiled by the TypeScript compiler. Fig 17 This is JavaScript.
Avoid using this in arrow functions unless you use it to solve problems such as the ones described on topic e. Otherwise, use regular anonymous functions to avoid surprises. An inner function inside of a method is not a method - it is a global function. In classes, if one of the purposes of using an inner function is to access members of the class, use an arrow function to redirect the this from the default global access, to the class environment access TypeScript adds a fake this to the arrow function and it resolves it based on the criteria we have seen in this chapter.
The purpose of the fake this in an arrow function is to set the this where the function is created, rather than setting it in the context where the function is invoked. This is the reason why it points to the global environment when it is created in the global environment even inside of a global child. It can also point to a class when it is created within the class. There is a lot more to arrow functions than what has been covered so far and I believe TypeScript is still developing more solutions.
Use the TypeScript Playground to experiment with other possibilities. Look at the results and analyze the JavaScript version to see what TypeScript is doing in the background. Learn code like if you were a mad scientist, test, test, and test some more.
The implementation selected when invoking the function is based on the type of arguments we feed to the function parameters when we invoke the function. We have seen this before when I discussed optional parameters. Since JavaScript functions accept any type of data as arguments, overloading is not as important as it is with other strongly typed [8] languages. However, TypeScript allows for overloading and that can help the compiler to warn the programmer for possible errors when he or she creates the project.
This is all possible thanks to type annotations. In other words, function overloading in TypeScript limits a function with type any parameters to the specific types available in the declared signatures, instead of authorizing all data types as parameters. Function overloading narrows the scope of the type any to the available authorized by the programmer signatures.
Keep in mind that since TypeScript allows for optional parameters in functions, you may find the optional parameters feature to be a better solution for the outcome you want to achieve, instead of using the overloading technique Im about to explain. Having options is a good thing!
The following example is totally bogus but it serves to illustrate how we can create function overloading in TypeScript without getting into more complex code.
In the code below, the interpreter looks at the data type and decides whether it should multiply or concatenate the result. To accomplish that, we want the function to accept either numbers or strings:. Lines 1 and 2 are function signatures. The reason to declare these signatures is to tell TypeScript that we intend to use the function for either numeric arguments or string arguments the calling types are limited to only one or the other.
Then, on line 3, I declared a function that accepts a type any parameter and, depending on which type of argument I pass when I invoke the function, JavaScript will execute the code differently. The execution is based on the if, else if statements. I could however just write the third function the one of type any and obtain the same results in JavaScript.
However, here is where you may find it useful: because I have declared two different function signatures above the one that implements the code, they conceptually limit the type of data that can be passed when I call the function.
For example, if I declare an array and pass in the array into the function, normally this would work because the function accepts any type. However, in TypeScript, this particular any type function is limited to number or string types due to the top two signature declarations. The TypeScript interpreter will warn me that I am about to make a potential mistake when I attempt to pass an array type as an argument to the function call. Because JavaScript does not support overloading, we implement overloading in TypeScript only to establish type safety rules for ourselves or for a programming team working on the project.
Create a program in TypeScript that converts temperatures from Fahrenheit to Celsius. You can find a link to my own code at the end of the instructions.
You will see that TypeScript underlines the value in red and when you hover with your mouse over it, you will read the following error: Argument of type string is not assignable to parameter of type number. See my code sample here: [37] jsplain. On the next project, we are going to reverse the function mechanism so that it converts from Celsius to Fahrenheit.
Project 2. See my code sample here: [38] jsplain. Use a second parameter that defaults to the character "f" as an argument. The idea is that when we enter the temperature argument to be converted, it automatically calculates a conversion from Fahrenheit to Celsius.
However, if we enter a second argument "c" in this case , it calculates a conversion from Celsius to Fahrenheit. On the other hand, if we enter another character instead of "f" or "c", it will alert that the conversion is not supported.
Name the first parameter x and make it of type number. Name the second parameter y, make it of type string and assign a default value of "f" to it. In this case, alert the result of a conversion from Fahrenheit to Celsius remember that we are using x as the variable name this time. In this case, alert the result of a conversion from Celsius to Fahrenheit use x as the numeric variable.
The following link takes you to my own code sample here: [39] jsplain. You can create additional else if clauses in the middle, or replace the conditional statements with a switch statement for a cleaner outcome.
This is what I did for my samples. To save them as TypeScript code, use the. For now, we really want the. In the above example, when function myColor is invoked, it displays red as a result which is the value in the inner variable color.
However, when we alert color by itself, it displays blue because it is the value in the global variable color. This shows that when we initialize a variable inside of a function by using var , the variable remains private and it does not conflict with variables outside of the function with similar names. This privacy does not apply to other code blocks in JavaScript syntax.
In JavaScript, functions are the only code block that allows for private variables. In the upcoming JavaScript 6 version, this will change but this feature is not yet supported across browsers. In the future, privatization in code blocks will be done with the keyword let instead of var. TypeScript allows for the usage of let by refactoring the code so it becomes compatible to all browsers.
This will be demonstrated in the next few topics. Planning for block scope Although not yet recognized by browsers, TypeScript allows the usage of let when we create our code project.
This implementation sometimes works and sometimes it does not make a difference when it comes to the final JavaScript transpiling result; it all depends on the context. However, even if block scope is not supported in every script context, it is good practice to use let whenever we create temporary variables. The same principle applies when we declare variables inside of code block groups because it implicitly tells TypeScript of our intentions, and TypeScript reward us with code checking as we continue to program the project.
In the next two examples, I will demonstrate what happens when implementing block scope in JavaScript by using let as a variable declaration prefix, instead of var:. In the above example, the second var x reassigns the first var x from to 7, and based on the fact that we used var the second time, this may have been done by mistake.
In other words, perhaps we did not know that x already existed and we just erased its original value.
0コメント