JavaScript basic Series (variables and types)

Source: Internet
Author: User
Tags define function

The following JavaScript is referred to as JS

Variable refers to the data that can be modified.

Variable this word in all programming languages are the most critical, the most common existence, in JS is no exception, so to understand the variable is particularly important, must pay attention to, in order to deep into the variable must first understand what the data type is what it plays in our actual coding, the next one by one to explain.

(This passage to 0 basic learning readers) many beginners will have doubts, what is the variable is what, what can be done? One of the simplest examples: you want to write code to achieve the function of subtraction, then the first premise is that you have to have two numbers, but the computer does not know the number, then you need to use the computer can understand the code to save the two numbers, that is what we call the definition of variables, here is still very peripheral , to use the professional terminology is to translate the predetermined code into binary code through the interpreter, then it is a very low-level problem we do not need to consider, just need to know: "The program is generated by code, the code is pre-designed by the computer to recognize the operation, we do is to let the computer read people's thinking."

So how to define variables in JS:

Use Var to define a variable in JS

var a = 1;var B = 2;a+b//3a*b//2

var is the definition, a is the variable name, the number 1 is what we call a variable, the variable can be stored in the following seven kinds of types including functions, so we can think of the variable as an empty box as long as the code is in accordance with the rules can be stored in the key place to use it.

    • The composition of the variable: definition, name, variable.
    • Definition: Tell the computer I have variables here and help me to open a space in memory to store these (data)
    • Name: This variable must be given a codename to facilitate subsequent calls.
    • Variables: Put in the data we need to use, which can be seen as a small piece of a jigsaw puzzle.
Variable is the first word you communicate with the computer, but also the most used later, so you have to remember its writing details, the novice often forget to add semicolons (;) or the keyword is wrong to write these are the program error must be checked factors, need to pay attention to the development process in the future can avoid many detours, Here too, I often come across details that are easily overlooked.
    1. Keywords and variable names are written incorrectly!
    2. When a variable name is called after a function block, it is often because a letter is not written, it may take more than 10 20 minutes to find out why it was wrong. I followed the video or the document wrote how is the error? (Beginners are particularly prone to such errors!) So, in each error, first quickly browse through the variable name and the following call is really consistent.
    3. Key words are fixed so few in practice to remember a heart.
    4. In our daily writing code process, the most used is the predecessor left the code base, not all need to write themselves, this is the real role of various libraries, frameworks, it contains so many years through a variety of engineers to verify the function library, we must learn the basic key to understand the deep role of these libraries, In order to cope with the various needs of the work, various programming languages have its own library of functions, when there are a few identical libraries, such as array library, string word processing library, Math digital processing library, date time acquisition library, these libraries almost all programming languages are interoperability, eight not the first relationship, So we need to quickly understand these basic libraries after learning the basics, and do an in-depth analysis the next time we talk about libraries.
Here's the digression in Java (not JavaScript), if you want to define a numeric variable that needs to be defined with an int, the text needs to be defined by a string, such as int a = 1; String b = ' text '; In JavaScript, this is omitted, a VAR is taken care of, and its type is what you write, such as var a = ' text '; var B = 66, where A is automatically recognized as a type of text, B is automatically recognized as a numeric type, and other types are automatically recognized as well. After all this, take a look at the variable type now, and also the key knowledge that any programming language must face., only the data types in JS are analyzed. seven major types:
Read tip: You can take a cursory look at the boolean explanation, (we don't have direct access to the following types of code, except that the function type uses: functions to define)

Basic type:

Number: numeric; var a = 12;

string: literal; var a = ' text ';

Boolean:true,fales; Boolean value: true. false;

Compound type (aggregate type):

Array: arrays; var a = [1,2,3,4, ' text ', ' abc ']; it is a collection of data.

Functions: Function A () {}; It is a function block implemented through code.

Object: The first impression and then the object story

Special types:

NULL: null object; This object is not defined when the object is called

Undefined: Undefined, usually in case of program error

What is the use of these types? Numbers, text, do not say, through the two types of data to complete the presentation, using a number of types to complete a variety of algorithms.   Function functions are also called methods:is a logical function of the collection, in the previous chapter also said that the function can be placed all the code, and then in the aid of these code to form a function of processing capabilities, in the programming of the most used in addition to the variables must be a function, and then open an in-depth explanation, let us first put the necessary foundation to secure. Array Arrays:   is also a common data type in various programming languages, its function is to combine a series of data to be processed to store, It is important to note that the data that you want to take in the array must be retrieved through a loop (length) to use this property (As to what is the loop what is length you now just need to leave an impression on the good, temporarily do not go to the end, there will be detailed explanation ), and the array has a similar function of the JSON object in JS, which is stored in the form of key-value pairs. What is a key-value pair? Let's look at the comparison
    • JSON storage data, var a = {a:12,b: ' Hello ', c:22}; Stored as a key-value pair, like the map in Java, where a is the key, and 12 is the value,
    • Array arrays with data, var a = [1, 2, ' Hello ', 123];
 Never ignore square brackets [] with curly braces {}. Now we just do an understanding to see is not very clear also does not matter, in later chapter practice uses in to do the key cognition, the next Boolean value studies well to understand is to study this chapter the focus. Object objects:   In JavaScript all objects belong to the core concept: it is the Big brother in JS, but it does not refer to the specific who is the object, but a means, only when using an object you can be materialized (instantiated) through the new keyword to achieve, The more specific function blocks and attributes are then invoked through the point (.). In JS, any data can be regarded as an object, which is why I say it is a generic reason. So what can it do for us? First of all, the object is written in the same way as JSON, are stored by key-value pairs, the call to a specific object: The property is a key to invoke the value of it, and in the object can be placed functions to build our own function block, here we do a simple understanding (only need to know that the object is a collection of function blocks, Only you want to use the built-in features when you use them to materialize. null and undefined: To learn more about this article, you can read it directly. Boolean Boolean value type:   Its use is very critical, all the judgment, loop statement is to rely on it to normal execution, and a program if not judged can not be a complete program, in fact, in the vernacular said: " The program wants to have the ability to deal with things, must have the ability to distinguish between good and bad"So how do you get to know this Boolean type in depth?" I would like to recommend one of the industry's great God's article Boolean algebra-Nanyi; it is the right way to follow him to learn the front. Let's start with the two variables defined earlier
var a = 1;       Number type var b = ' text ';  Literal types are also called string types
A = = B; True they are equal to convert numbers to string type A = = B;//fales false They are not equal do not convert direct comparison
The above three equals sign (= = =) is one of the operators, called the comparer, and the comparator (>, <, >=, <=, = =, = =,! =,!==; greater than, less than, greater than or equal to, less than equals, type comparison,! Not equal to), double equals (= = ) and now see the three equals sign (= = =), which are not the focus of this chapter on operator analysis. Right now we have to pay attention to what the Boolean is going to be, which directly affects the clarity and certainty of the logic we write in the future, and the confidence in writing the judgment statement, so let's look at the condition of the Boolean value.
    1. When using a comparer, a Boolean value must appear.
    2. all values that appear separately in JS can be treated as Booleans , but have the following rules.
      • Undefined, null, 0, -0, NaN, "" will be converted to false; (check back to see if you want to use it later)
      • All other values, including all independent objects, functions, arrays, and variables, are converted to true;
Boolean: It has only two values, True and false, true and Fales, and the Judgment Statement if () is executed only if True (true), while else is executed in the false case and then explained. A program's ability to judge is derived from the flexible application of Boolean values, followed by the comparison operator, the logical operator in detail it, here first left a Boolean is the important impression okay? Section: You should have the following results after reading this chapter
    1. We know that variables and types are the entrance to the program.
    2. Writing code is not all the functions need to go to their own implementation, but to flexible use of the predecessors left us with the function of the collection (library), as well as learning the built-in objects.
    3. A function is a set of functions that can place any code, and you want the function to have functionality that must be preceded by a defined variable before the code logic is step-by-step.
    4. The function of a program is generated by a variety of judgments (not absolute).
    5. To make the program capable of judging, you must use the Boolean value True and False (which is absolute).
    6. Understanding the rules for the occurrence of Boolean values will allow you to avoid particularly detours.
    7. Seven Big data types, we just need to focus on: Numbers, text, Boolean values, arrays, functions, (and objects, which appear less in the base content, after which the object is being explained, now let the object in a higher level than the function block can be.) )
    8. Let the computer read our code must use the language pre-set keyword on the line description such as: Define variable var, define function functions, define the judgment statement if (), there are many later mentioned.
    9. There are many readers who can summarize themselves.
Hope you can have some gains and hope that you can get your comments thanks for reading!

JavaScript basic Series (variables and types)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.