Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
JS
What is it, it, it?
Js is one of the most popular scripting languages on the Internet
- It is a lightweight programming language
- It is a weakly typed language
- It is programming code that can be inserted into an HTML page
- It can be executed by all modern browsers
- It’s easy to learn !!!!!
It can do
Js is the best in the world (sure), so what can we do with it
Hold up a few chestnuts
- When we log in, JS will make some judgments about our input data
- Some of the beautiful animation effects in the web page can also be realized with JS
- , etc.
The most basic debugging method
As a qualified programmer, we should first learn to debug our own programs,
To debug and see what went wrong, the most common thing we front-end programmers use is printing, right
var a = 'I love Grey Wolf';
console.log(a);
Copy the code
Begin formal study
Where can we put the JS code
We’ve got HTML and CSS so where can we put JS?
- The inline type
<input onclick="Alert (' Rui Rui women's Dress ')">
Copy the code
- embedded
<script>
alert('js');
</script>
Copy the code
- External introduction
<script src="./index.js"></script>
Copy the code
Our old friend, the variable
Everyone has learned c language, so variables also have a certain understanding, but in JS variables are loosely typed, can save any value
Uninitialized variables hold a total of special values -undefined
Naming conventions for variables
Js variables have naming conventions
- Variables must start with a letter
- Variable names are case-sensitive (A and A are different variables)
let a;
let 1a;
let A;
Copy the code
Declare a variable
As you can see above, there are three ways to declare a variable. The main difference is the scope of the variable
- Var:
- Let:
- const:
The data type
- Dynamic type
Dynamic typing is when the same variable can be used as different types
int a;
float c;
char a;
Copy the code
var a = 1;
var b = "1"
var c = "123"
typeof(a)
typeof(b)
typeof(c)
Copy the code
- string
A string is a variable that stores characters.
A string can be any text in quotes, either single or double quotes:
var carname="my name id cxy";
var carname='my name id cxy';
Copy the code
Strings can also be concatenated that is, two strings can be concatenated into one string, 1+1=1
var a = "1"
var b = "2"
console.log(a+b)
Copy the code
Of course, we can concatenate variables in string concatenation to solve the requirement that some content should change with data
var a = 20
console.log('cxy'+a+', ')
Copy the code
There is no difference between single and double quotation marks in syntax, but only in specification. 3. Digital
Js has only one numeric type, which is very different from C
var a = 100;
typeof(a);
var b = 3.14;
typeof(b);
Copy the code
- Boolean
Booleans have only true and false values
Arrays are something that we’re going to use a lot in our programs
var array = new Array(a); array[0] = 1;
array[1] = "qinli"
console.log(array);
Copy the code
- Undefined
In JavaScript, a variable has no value; its value is undefined. Typeof also returns undefined.
var a;
console.log(a)
/ / output is undefined
Copy the code
- Object 👩 🦰 🧑
Does everyone know what the object is 😛
In fact, it’s similar to the structure of C for example, if I declare a student’s structure, give it some properties, like name, age, major, etc. So we declare an object in JS, give it some properties like name, age, gender.
var person={
name : "John".id : 5566
};
console.log(person.name)
console.log(person["name"])
Copy the code
- Null
NULL is a special variable
typeof(null)
// The output is object
Copy the code
This is because null represents a pointer to an empty object. The difference from undefined is that it is null, and undefined is undefined
Our right-hand man — functions
Function declarations and function expressions
- Function declaration
function myname(){
var name = "cxy"
return name;
}
Copy the code
- Functional expression
var myname = function(){
var name = "cxy"
returnname; }}Copy the code
What’s the difference?
Probably also read the difference between it
The function declaration will be read by the parser first, and the function expression will wait “a long, long time” to be parsed, and an error will be reported if called.