search
HomeWeb Front-endJS TutorialExplanation of JavaScript related content

1. JavaScript Introduction:

JavaScript is the most popular scripting language on the Internet, and all modern HTML uses JavaScript. Since it is a scripting language, it has three characteristics:

(1) Weak type;

(2) Interpreted type Language (no compilation required);

(3) Line by line execution, if one line of code is wrong, subsequent code blocks will not continue to execute;

(4) The <script> tag can be directly embedded into the HTML file. The position is arbitrary. <span style="background-color:rgb(255,255,255);">It is usually placed below the modified content or in the head tag, but writing it as a separate js file is conducive to the separation of structure and behavior</script>

2.JavaScript content (with pictures):

##​


## Where ECMAScript is the core of JavaScript;

DOM It is the document object model (using js to operate the web page);


BOM is the browser object model (using js to operate the browser)


3. JavaScript information output:

(1) alert() method: in the form of a prompt box on the page Output, for example;


<script>
    alert("hello,javascript")
</script>
(2) console.log() method: output information on the console, for example:

console.log("hello,javascript")

(3) document.write() : Write the content directly in the HTML page, for example:

document.write("hello,javascript")

4. JavaScript variable: ## Unlike Java, ECMAScript The variables in have no specific type. When defining a variable, only use the var operator. It can be initialized to any value. The initialization format of the variable: var variable name = variable value; example:

var a = "hello";
var b = 123;

If you want to define multiple variables, you can write multiple variables on one line and separate them with commas; example:

var a = "你好",
    b = 123,
    c = "hello";
Variable rules for variable names:

(1)

consists of letters, numbers, underscores, and $ symbols

(2) It cannot start with a number, and it is not recommended to start with an underscore;

(3 ) Strictly case-sensitive;

(4) Cannot be keywords and reserved words

5. JavaScript data type:

JavaScript can be divided into two types: primitive data types and reference data types:

(1) Primitive data types: Number, String, Boolean, undefined, null

Number

: Numeric type, is a number, including positive numbers, negative numbers, integers, decimals, 0, NaN, Infinity (positive infinity), -Infinity (negative infinity); Note:

NaN: abbreviation of not a number, indicating that the value is not a numerical value (also belongs to Number)

    String字符串:用双引号""或单引号''包起来的0个或多个字符,如果引号中什么也没有,那么这个字符串被称为空字符串

    Boolean布尔型:包含true:表示真(成立)和false:表示假(不成立)两个值

    undefined表示变量未定义,或变量被定义出来,但是没有被赋值

    null表示一个变量没有指向任何一片存储空间,即变量存在,但是里面是空的,类似于Undefined

    (小提示:在Chrome浏览器控制台输出时,会发现Number类型为深蓝色,String为黑色,Boolean为浅蓝色,undefined和null都为浅灰色)

    (2)引用数据类型:

    Object(对象),Array(数组),Date(日期),RegExp(正则)。。等等

    (3)如何查看一个变量的数据类型(typeof 运算符):        

             数值型数据:返回值为number   

console.log(typeof 123)   //输出number

             字符串型数据:返回值为string

console.log(typeof "你好")  //输出string

             布尔型数据:返回值为boolean

console.log(typeof true/false)    //输出boolean

             Undefined:返回值为undefined

console.log(typeof undefined)   //输出undefined

             Null:返回值为Object(历史遗留问题,说明null也是一个对象)

console.log(typeof null)     //输出object

             NaN:返回值为number

console.log(typeof NaN)    //输出number

6.JavaScript 数据类型的转换:

    (1)在使用加法(+)运算符时,任何数据与字符串类型数据相加都为字符串类型数据;

console.log("你好" + 123)    //输出"你好123"

        注(简单理解): 在JavaScript 中空字符串""转换为false,非空字符串转换为true(除“0”,“1”外);

                false转换为 0 或“0”,true转换为 1 或“1”;

                做逻辑判断的时候,null,undefined,""(空字符串),0,NaN都默认为false;

                ==在比较的时候可以转换数据类型,===是严格比较,只要类型不匹配就返回false;

                    其实 == 的比较确实是转换成字符串来比较但,但是在布尔型转换为字符串之前,要先转换成 Number

console.log("123" == true)    //输出false
console.log("1" == true)     //输出true
console.log("" == true)     //输出false
console.log(1 == true)     //输出true

console.log("" == false)    //输出true
console.log(&#39;123&#39; == false)   //输出fasle
console.log(&#39;0&#39; == false)    //输出true
console.log(0 == false)    //输出true

console.log(&#39;1&#39; == 1)     //输出true
console.log(&#39;0&#39; == 0)	  //输出true
console.log(-true)     //输出-1

(2)parseInt:将字符串转换成整数(只识别字符串中的数值):

        注:会忽略字符串中的前后空格(当数值后的空格后面还有数值时,将不会再识别);

               能够正确识别正负号,即保留正负号;

               在转换时,遇到非数值型的字符就会停止转换;

               如果字符串的第一个字符是非数值型的,那么转换的结果为NaN;

console.log(parseInt("123"))    //输出123
console.log(parseInt(" 1 2"))    //只会输出1
console.log(parseInt(-123))     //输出-123
console.log(parseInt("hello"))    //输出NaN
console.log(parseInt(true))       //输出NaN
console.log(parseInt("123hello"))    //输出123,后面非数值型不会识别
console.log(parseInt(" 1 "))     //输出1,忽略空格

(3)parseFloat:将字符串转换成小数(识别小数点,注意事项同上)

console.log(parseFloat("123.55"))    //输出123.55
console.log(parseFloat(".1hello"))    //输出0.1

(4)Number:将其它类型的数据转换成数值型,注意被转换的数据必须是纯数值构成,否则无法转换,其它注意事项同上

console.log(Number(true))	//1
console.log(Number(false))    //0
console.log(Number(null))    //0
console.log(Number("123hello"))    //NaN
console.log(Number("12.22"))    //12.22
console.log(Number(undefined))    //NaN

(5)页面中的信息框:

        alert(),弹出个提示框,只有确定;

window.alert("今天天气很好")


        confirm(),弹出个确认框,有确定和取消;

window.confirm("今天心情也很好")


        prompt(),弹出个输入框,可以输入内容;

window.prompt("password","请输入密码")



JavaScript的基础暂时先写到这里,后续都会补上。。。

本文讲解了JavaScript相关的内容讲解,更多相关之请关注php中文网。

相关推荐:

关于HTML基础的讲解

$.ajax+php实战教程之下拉时自动加载更多文章原理讲解

关于zx-image-view图片预览插件,支持旋转、缩放、移动的相关操作

The above is the detailed content of Explanation of JavaScript related content. For more information, please follow other related articles on the PHP Chinese website!

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
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

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.

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 Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use