当您浏览 Internet 时,您与之交互的网页会使用三种技术呈现给您:HTML、CSS以及我们将在本文中讨论的内容 — JavaScript。
在这三者中,JavaScript是最先进的。它为网页添加了动态功能,使它们更加复杂和交互。没有JavaScript,您最喜欢的一些网站将无法运行。
在本文中,让我们分解一下JavaScript是什么、它是如何工作的,以及它在网上的许多用途。
什么是 JavaScript?(What Is JavaScript?)
JavaScript是一种脚本语言。HTML是其最受欢迎的亲戚之一,是一种标记语言。两者之间的主要区别在于标记语言是结构化的并且准备页面的外观和设计。标记语言在功能上是表现性的,而脚本语言是逻辑和算法的。
通俗地说,HTML改变了网页的外观,而JavaScript实际上提供了计算功能。在HTML中,甚至无法计算一个简单的等式,例如 1 + 1。JavaScript(JavaScript)可以做到这一点,还有更多。
JavaScript可以在服务器端和客户端实现。客户端JavaScript是最常见的,其中JavaScript代码通过HTML引用并由 Web 浏览器解释。这段代码不仅可以改变页面的外观,还可以做很多事情,比如动态生成HTML内容、请求用户输入等等。
简而言之,JavaScript是一种轻量级的解释性脚本语言,可以增强网站的交互性。
JavaScript 是如何在线使用的?(How Is JavaScript Used Online?)
与几乎所有脚本语言一样,可能性仅限于您能想到的。如果您有技能并且可以梦想,那么您应该能够做到。
但是,有一些非常常见的JavaScript脚本,如果没有这些脚本,Web 就不一样了。让我们讨论一些最流行的JavaScript用例。
表单验证
如果您曾经在填写注册表单时却被告知您的用户名或密码太短,那就是JavaScript在起作用!
表单(Form)验证可以用多种不同的脚本语言来执行,但JavaScript允许它动态地完成并且无需发布任何数据。这可以通过降低带宽来降低服务器成本,并且是防止用户感到沮丧的好方法。
提交表格并在事后被告知您的信息无效,这绝不是一件有趣的事。JavaScript可以即时执行此操作。
转换器和计算器
无论是Fahrenheit到Celsius,USD到EUR等等,动态转换数据都是JavaScript擅长的。如果您曾经学习过JavaScript课程,那么您的前几节课中的一个可能就是创建诸如利率计算器(interest rate calculator)之类的东西。这是一个很好的学习体验,介绍了JavaScript的许多基础知识。
客户端JavaScript是处理此类计算的最佳解决方案之一。
随机发生器
Math.random () 是许多JavaScript爱好者最喜欢的函数,它非常有用。它允许创建一个介于 0 和 1 之间的随机浮点数,这使得许多聪明的脚本成为可能。
这种最流行的脚本之一是随机(random )密码(p)生成器(assword generator)。您可以在整个网络上找到这些,它们是随机密码生成的可靠来源,因为客户端JavaScript与服务器没有交互,因此它永远不会在幕后缓存或保存。
游戏
没错——JavaScript甚至可以用来编写你自己的游戏。虽然客户端JavaScript不允许支持任何类型的在线合作体验,但单人游戏或两个用户可以在同一个键盘上玩的游戏是完全可能的。
下面是几个经典的例子:
JavaScript是一种非常优秀的脚本语言,即使对于不那么严肃的项目也适用!
如果您已经精通HTML和CSS,那么JavaScript是一个很好的下一步,可以真正增强您的编码游戏。如果您对编码不感兴趣,那么本文应该有助于了解这种脚本语言为数百万个网站带来的功能。没有JavaScript(JavaScript),互联网就不一样了。
HDG Explains: What Is JavaScript & What Is It Used For Online?
When you browse the internet, the web pages you interact with arе presented to you using three technologies: HTML, CSS, and whаt wе’ll discuss in this article—JavaScript.
Of the three, JavaScript is the most advanced. It adds dynamic functionality to web pages, which make them much more complex and interactive. Without JavaScript, some of your favorite websites wouldn’t be possible.
In this article, let’s break down what JavaScript is, how it works, and the many uses for it online.
What Is JavaScript?
JavaScript is a scripting language. HTML, one of its most popular relatives, is a markup language. The major difference between the two is that markup languages are structural and prepare the look and design of a page. A markup language is functionally presentational, whereas a scripting language is logical and algorithmic.
In layman’s terms, HTML changes the way web pages look while JavaScript actually offers computational functionality. In HTML, there’s no way to even evaluate a simple equation such as 1 + 1. JavaScript can do that and so much more.
JavaScript can be implemented on both the server and client side. Client-side JavaScript is most common, where JavaScript code is referenced via HTML and interpreted by the web browser. This code can do a lot more than just change the appearance of a page, such as dynamically generate HTML content, ask for user input, and more.
To put it short, JavaScript is a lightweight, interpreted scripting language that can enhance the interactivity of a website.
How Is JavaScript Used Online?
As with practically every scripting language, the possibilities are limited only to what you can come up with. If you have the skills and you can dream it, you should be able to make it.
However, there are some very common JavaScript scripts that the web would not be the same without. Let’s discuss a few of the most popular use cases for JavaScript.
Form Validation
If you’ve ever been filling out a signup form only to be told that your username or password was too short, that was JavaScript in action!
Form validation can be performed in a number of different scripting languages, but JavaScript allows it to be done dynamically and without posting any data. This can cut down server costs through lowered bandwidth, and it’s a great way to prevent frustration on the side of the user.
It’s never fun to submit a form and be told afterwards that your information wasn’t valid. JavaScript can do this on the fly.
Converters & Calculators
Whether it be Fahrenheit to Celsius, USD to EUR, or the like, dynamically converting data is something JavaScript excels at. If you ever take a course on JavaScript, one of your first few lessons will likely be to create something such as an interest rate calculator. It’s a great learning experience that introduces many of the fundamentals of JavaScript.
Client-side JavaScript is one of the best solutions for when it comes to handling calculations such as these.
Random Generators
Math.random() is a favorite function for many JavaScript lovers, and it’s extremely useful. It allows the creation of a random, floating-point number between 0 and 1, which enables a lot of clever scripts.
One of the most popular scripts of this kind is a random password generator. You can find these all over the web, and they’re a trustworthy source of random password generation since client-side JavaScript has no interaction with the server, so it’s never cached or saved behind the scenes.
Games
That’s right—JavaScript can even be used to code your own games. While client-side JavaScript won’t allow support for any kind of online, co-operative experience, single-player games or games that can be played by two users at the same keyboard are entirely possible.
Here are a few classic examples:
JavaScript is a seriously good scripting language that works even for not-so-serious projects!
If you’re already well-versed in HTML and CSS, JavaScript is a great next step that can really enhance your coding game. If coding isn’t your interest, this article should help in appreciating the functionality this scripting language has brought to millions of websites. The internet wouldn’t be the same without JavaScript.