Hackerthink logo

THE MOST POPULAR SITE FOR DEVELOPERS.

JavaScritp Intoduction

JavaScript is the most popular scripting language on the Internet. This language can be used in HTML and web, and can be widely used in servers, PCs, laptops, tablets, and smartphones.

JavaScript is a scripting language

JavaScript is a lightweight programming language.

JavaScript is programming code that can be inserted into HTML pages.

After JavaScript is inserted into the HTML page, it can be executed by all modern browsers.

JavaScript is easy to learn.

What you will learn

Below are the main things you will learn in this tutorial.

JavaScript: write directly to the HTML output stream

Example

document.write (" <h1>This is a heading</h1> ");
document.write (" <p>This is a paragraph.</p> ");
You can only use document.write in HTML output. If you use this method after the document is loaded, the entire document will be covered.

JavaScript: Reacting to events

Example

<button type="button" onclick="alert('welcome!')">welcome!</button>

The alert() function is not commonly used in JavaScript, but it is very convenient for code testing.

The onclick event is just one of the many events you will learn about in this tutorial.

JavaScript: Change HTML content

Using JavaScript to process HTML content is a very powerful feature.

Example

x=document.getElementById(" demo ");   // Find element
x.innerHTML=" Hello JavaScript ";     // change content

You will often see document.getElementById(“ some id “) . This method is defined in HTML DOM.

The DOM ( D ocument O bject M Odel) (Document Object Model) is the official W3C standard for accessing HTML elements.

You will learn about HTML DOM in several chapters of this tutorial.

JavaScript: Change HTML style

Changing the style of HTML elements is a variant of changing HTML attributes.

Example

x=document.getElementById("demo")  //Find element
x.style.color="#ff0000";           //Change style

JavaScript: Validate input

JavaScript is often used to validate user input.

Example

if isNaN(x) {
    alert("Not a number");
}

The above examples are just ordinary verification. If you want to use it in a production environment, you need to strictly judge. If you enter a blank or continuous blank isNaN, it cannot be distinguished. You can add regular to judge (the following chapter will explain):

Example

if(isNaN(x)||x.replace(/(^s*)|(s*$)/g,"")==""){
    alert("不是数字");
}

Do you know?

JavaScript and Java are two completely different languages, both conceptually and by design.
Java (invented by Sun) is a more complex programming language.

ECMA-262 is the official name of the JavaScript standard.

JavaScript was invented by Brendan Eich. It appeared in Netscape in 1995 (the browser has stopped updating) and was adopted by ECMA (a standards association) in 1997.

ECMAScript version

JavaScript has been standardized by ECMA (European Computer Manufacturers Association) through ECMAScript.

years name description
1997 ECMAScript 1 First version
1998 ECMAScript 2 Version change
1999 ECMAScript 3 Add regular expressions
to add try / catch
ECMAScript 4 Not released
2009 ECMAScript 5 Add “strict mode”,
add JSON support for strict mode
2011 ECMAScript 5.1 Version change
2015 ECMAScript 6 Add classes and modules
2016 ECMAScript 7 Add exponential operator (**)
add Array.prototype.includes