search
HomeWeb Front-endJS TutorialECMAScript basic knowledge_javascript skills

The language features of ECMAScript, one of the cores of JavaScript, have many similarities with Java, C, and Perl. Many of the features are borrowed from these languages, and there are also many differences between them. Here are some basic features of ECMAScript.

--Like Java, ECMAScript is case-sensitive, the format of comments is the same, the code block is determined by {}, the original data type is stored on the stack, and the reference to the object is stored in the heap
--ECMAScript is A loose language, ECMAScript declares variables through the var operator, and the type is not limited. For example, var n = 25, then n is a numeric type, var n = "string", then n is a String type
-- in each After a line of code, you do not need to write a semicolon. ECMAScript automatically considers the end of the line to be the end of the line of code. Variables in ECMAScript do not need to be initialized. The system will automatically complete the initialization operation behind the scenes
--The same variable can be assigned to different Type of data; the first character of a variable can only be a letter, underscore, or $, and other characters can be an underscore, $, or any letters, numbers, or characters
- Like other languages, variables are best followed Camel case notation, or Pascal notation, or Hungarian notation
- Unlike most languages, ECMAScript variables do not need to be declared before use. The system will automatically declare the variable as a global variable, such as var m = " Good " ; n = m " Morning " ; alert(n) output structure is " Good Morning "
-- In most languages, String is an object, but in ECMAScript it is a primitive data type

Primitive data types

There are five ECMAScript primitive data types: Undefined, Null, Boolean, Number, and String.

typeof - Determines the data type of variables and values. There are usually five types: undefined, boolean, number, string, and object.
Undefined—When a variable is declared but not initialized, or a function does not explicitly return a value, the variable or function is of type Undefined.
Null—undefined is a derivative of null. When the value representing an object does not exist, the object returns null.
Boolean—contains two values, true and false, false is not equal to 0, but 0 can be converted to false.
Number—can define 32-bit integer data or 64-bit floating point data. When defining a numeric type variable, add 0 in front of the number to indicate octal, and add 0x to indicate hexadecimal. The results returned after their calculations are uniformly decimal. A floating-point type variable can be defined by var f = 1.0. Interestingly, before f is used in calculations, it is actually stored as String type. When the floating point type data is large or small (can be shifted forward and backward by six bits), the E notation will be used to represent the floating point data, and a maximum of 17 bits of data can be stored. In addition, the isFinite() method can determine whether a value is limited, and the isNaN() method can determine whether a data is a non-numeric type.
String—String is a primitive data type in ECMAScript and is the only data type that has no space size limit. Unlike Java, var s = "javascript" and var s = 'javascript' are both legal expression methods.

Data conversion

Converting between different data types is an important feature of any programming language. ECMAScript provides a series of simple methods to achieve data conversion for most data types. All provide simple conversion methods, and there are some global methods to complete complex conversions. No matter which method is used, data conversion in ECMAScript is very simple.

Boolean, number and string data types are primitive data types, but they are also pseudo-objects (how to explain pseudo-objects in ECMAScript? How is the operating mechanism still unclear? If anyone knows, please give an answer), with Your own properties and methods can be converted to string type through the toString() method. ECMAScript defines that all objects, whether pseudo-objects or real objects, can implement the toString() method. String is listed as a pseudo-object, and naturally also has a toString() method. When converting numeric data to string, you can add 2, 8, and 16 parameters to the toString() method to realize data output in different bases, such as var n = 10; alert(n.toString(2)) output is 1010, alert(n.toString(8)) output is 12, n.toString() and n.toString(10) are the same.

ECMAScript provides two methods to convert string type into numeric type: parseInt() and parseFloat(). Other type conversions will return NaN (Not a Number).

Type Casting

ECMAScript data type conversion can usually be achieved through three methods: Boolean(value), Number(value) and String(value), which usually produces some unexpected results result.

Boolean



var b1 = Boolean( "" ); // false–empty string
var b2 = Boolean( " hi " ); // true –non-empty string
var b3 = Boolean( 100 ); // true–non-zero number
var b4 = Boolean( null ); // false-null
var b5 = Boolean( 0 ) ; // false-zero
var b6 = Boolean( new Object()); // true–object

Number



Number( false ) 0
Number( true ) 1
Number(undefined) NaN
Number( null ) 0
Number( " 5.5 " ) 5.5
Number( " 56 " ) 56
Number( " 5.6 .7 " ) NaN
Number( new Object()) NaN
Number( 100 ) 100

String

String() can realize direct conversion of all types of data, and The difference with using toString() is that String() can convert null or undefined data into string.

Reference type

ECMAScript actually does not have classes in the traditional sense. It just defines objects to be equivalent to classes in other languages. I am still vague about this point. I may understand it in the future. In the article, it is still explained by "category".



var ob = new Object();

The above defines an instance of Object object. This syntax is similar to Java. When there are parameters, parentheses are needed to quote. When there are no parameters, the parentheses can be removed. Since the ECMAScript language is relatively loose, whether it is the basic grammar mentioned above or the grammatical knowledge mentioned later, we should try our best to agree on our own code format according to certain writing standards, and should not give full play to the loose characteristics of the language.

Object class

The Object class is similar to the java.lang.Object class in Java. It is the base class for all other classes in ECMAScript. It has the following attributes:

Constructor—a reference to a function that creates an object. For the Object class, this reference points to the local Object() method.
prototype—a reference value of the prototype object in the object.

Methods owned by the Object class:

hasOwnProperty(property) - Determine whether the property attribute exists in the object, the property data type is string
isPrototypeOf(object) - Determine whether an object is The prototype of another object
propertyIsEnumerable(property) - Determines whether the given property can be enumerated using the for statement
toString() - Returns the original type string of the object
valueOf() - Returns the appropriate value of the object Primitive value. For most classes, the returned value is the same as toString()
Every property and method of Object class is overridden by other classes

Boolean class

Define method var ob = new Boolean(true); ob is a reference of Boolean primitive data type. When using Boolean objects, you need to note that all objects will automatically change to true, so var ob1 = new Boolean(false); var ob2 = ob1 && true; Finally, the value of ob2 is true, not false. In general, this situation can be avoided by using the Boolean primitive data type.

Number class

Define method var o = new Number(15);
Get the value of the original data var n = o.valueOf();

Number class There are some methods specially designed for numeric values:



alert(o.toFixed( 2 )); // Output 15.00
alert(o.toExponential( 1 )) ; // Output 1.5e 1

When you are unsure whether toFixed or toExponential should be used, you can use the toPrecision method to get the value:



alert(o.toPrecision( 1)); // Output 2e 1
alert(o.toPrecision( 2)); // Output 15
alert(o.toPrecision( 3)); // Output 15.0

String Class

The String class is a complex reference type. Here are only some common methods, many of which imitate java.lang.String:



var s = new String( " Good Morning " );
alert(s.valueOf() == s.toString()); // Output true
alert(s.length); // Output 12
alert(s.charAt( 1 )); // Output o
var sr = s.concat( " ! " ); alert(sr); // Output Good morning!
alert(s.indexOf( " o " ); // Output 1
alert(s.lastIndexOf( " o " ); // Output 6
alert(s.localeCompare(Good morning)); // Output 0
alert( s.localeCompare(Apple)); // Output 1
alert(s.localeCompare(House)); // Output -1
alert(s.slice(2)); // Output od morning
alert(s.substring( 2 )); // Output od morning
alert(s.slice( 2 , - 5 )); // Output od mo
alert(s.substring( 2 , - 5 )); // Output Go
alert(s.toUpperCase()); // Output GOOD MORNING
alert(s.toLowerCase()); // Output good morning

In addition, all The methods of the String class can also be used on the String primitive data type because it is a pseudo object.

instanceof

The instanceof operator is similar to typeof. The difference is that instanceof needs to explicitly specify whether the object belongs to a specific type. For example



var s = new String( "Good morning ! " );
alert(s instanceof String);

Operators and statements

Most operators and statements in ECMAScript are similar to Java, but there are also some unique ones, such as label statement, with statement, for-in statement, etc.

Functions

Functions are the core of ECMAScript, a set of code statements that can be run at any time and anywhere.



function functionName(arg0, arg1, … , argN) {
statements
}

When function has no return value or there is no value after the return statement , the function will actually be defined as undefined by the system. When the function returns a value, the function does not need to be explicitly specified as a certain data type.

About overloading

Overloading is one of the basic features of object-oriented languages, but ECMAScript functions cannot be overloaded. Two identical functions can be defined in the same scope. When a function is called, the last function comes into play. This feature is cumbersome, but similar functions to overloading can be implemented through arguments objects.



function func() {
if (arguments.length == 1 ) {
alert(arguments[ 0 ] 5 );
} else if (arguments .length == 2) {
alert(arguments[ 0 ] arguments[ 1 ]);
}
}

func( 5 ); // Output 10
func( 10 , 15 ); // Output 25

As mentioned earlier, two identical functions can be defined in the same scope. When the function is called, the last function takes effect.



function func(i) {
alert(i 10 );
}
function func(i) {
alert(i 20);
}
func( 5 ); // Output 25

It can be seen that the last function is called so that the data result is 25. If the Function class is used to define the above two functions, then It might be clearer why the last function is used.



var func = new Function(“i”, “alert(i 10)”);
var func = new Function(“i”, “alert(i 20)”) ");
func( 5 );

func points to another reference, so the value changes. Func exists as a reference to the function object and allows two variables to point to the same function .

There are many attributes and methods related to the Function class, such as length, toString(), valueOf(), etc. Among them, toString() is often used in debugging programs.

Original text: http://www.blogjava.net/flyingis/archive/2006/06/13/52484.html

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft