<h1 id="what-is-javascript">What is JavaScript</h1>
<p>JavaScript is a programming language initially designed to interact with elements of web pages. In web browsers, JavaScript consists of three main parts:</p>
<ul>
<li>ECMAScript provides the core functionality.</li>
<li>The <a href="https://www.javascripttutorial.net/javascript-dom/">Document Object Model (DOM)</a> provides interfaces for interacting with elements on web pages</li>
<li>The <a href="https://www.javascripttutorial.net/javascript-bom/">Browser Object Model (BOM)</a> provides the browser API for interacting with the web browser.</li>
</ul>
<p>JavaScript allows you to add interactivity to a web page. Typically, you use JavaScript with HTML and CSS to enhance a web page’s functionality, such as <a href="https://www.javascripttutorial.net/javascript-dom/javascript-form-validation/">validating forms</a>, creating interactive maps, and displaying animated charts.</p>
<p>When a web page is loaded, i.e., after HTML and CSS have been downloaded, the JavaScript engine in the web browser executes the JavaScript code. The JavaScript code then modifies the HTML and CSS to update the user interface dynamically.</p>
<p>The JavaScript engine is a program that executes JavaScript code. In the beginning, JavaScript engines were implemented as interpreters.</p>
<p>However, modern JavaScript engines are typically implemented as just-in-time compilers that compile JavaScript code to bytecode for improved performance.</p>
<h2 id="client-side-vs-server-side-javascript">Client-side vs. Server-side JavaScript</h2>
<p>When JavaScript is used on a web page, it is executed in web browsers. In this case, JavaScript works as a client-side language.</p>
<p>JavaScript can run on both web browsers and servers. A popular JavaScript server-side environment is <a href="https://www.javascripttutorial.net/nodejs-tutorial/">Node.js</a>. Unlike client-side JavaScript, server-side JavaScript executes on the server that allows you to access databases, file systems, etc.</p>
<h2 id="javascript-history">JavaScript History</h2>
<p>In 1995, JavaScript was created by a Netscape developer named <a href="https://en.wikipedia.org/wiki/Brendan_Eich">Brendan Eich</a>. First, its name was Mocha. And then, its name was changed to LiveScript.</p>
<p>Netscape decided to change LiveScript to JavaScript to leverage Java’s fame, which was popular. The decision was made just before Netscape released its web browser product Netscape Navigator 2. As a result, JavaScript entered version 1.0.</p>
<p>Netscape released JavaScript 1.1 in Netscape Navigator 3. In the meantime, Microsoft introduced a web browser product called the <a href="https://en.wikipedia.org/wiki/Internet_Explorer">Internet Explorer</a> 3 (IE 3), which competed with Netscape. However, IE came with its own JavaScript implementation called <a href="https://en.wikipedia.org/wiki/JScript">JScript</a>. Microsoft used the name JScript to avoid possible license issues with Netscape.</p>
<p>Hence, two different JavaScript versions were in the market:</p>
<ul>
<li>JavaScript in Netscape Navigator</li>
<li>JScript in Internet Explorer.</li>
</ul>
<p>JavaScript had no standards that governed its syntax and features. And the community decided that it was time to standardize the language.</p>
<p>In 1997, JavaScript 1.1 was submitted to the <a href="https://www.ecma-international.org/">European Computer Manufacturers Association</a> (ECMA) as a proposal. <a href="https://www.ecma-international.org/memento/tc39-m.htm">Technical Committee #39</a> (TC39) was assigned to standardize the language to make it a general-purpose, cross-platform, and vendor-neutral scripting language.</p>
<p>TC39 came up with ECMA-262, a standard for defining a new scripting language named ECMAScript (often pronounced Ek-ma-script).</p>
<p>After that, the International Organization for Standardization and International Electrotechnical Commissions (ISO/IEC) adopted ECMAScript (ISO/IEC-16262).</p>
<h2 id="javascript-overview">JavaScript overview</h2>
<p>To <a href="https://www.javascripttutorial.net/javascript-variables/">define a variable</a> in JavaScript, you use <code>var</code> keyword. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">var</span> x = <span class="hljs-number">10</span>;
<span class="hljs-keyword">var</span> y = <span class="hljs-number">20</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>ES6 added a new way to declare a variable with the <code>let</code> keyword:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> x = <span class="hljs-number">10</span>;
<span class="hljs-keyword">let</span> y = <span class="hljs-number">20</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>There are differences between <a href="https://www.javascripttutorial.net/es6/difference-between-var-and-let/"><code>var</code> and <code>let</code></a>. And it’s a good practice to use the <code>let</code> keyword to declare variables.</p>
<p>To declare a <a href="https://www.javascripttutorial.net/javascript-function/">function</a>, you use the <code>function</code> keyword. The following example defines a function that calculates the sum of two arguments:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">add</span>(<span class="hljs-params"> a, b </span>) {
<span class="hljs-keyword">return</span> a + b;
}<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>To call the <code>add()</code> function, you use the following syntax:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> result = <span class="hljs-title function_">add</span>(x, y);<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>To log the result into the console window of the web browser, you use the <code>console.log()</code> :</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(result); <span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>Now, you should see <code>30</code> in the console window.</p>
<p>JavaScript provides you with condition statements such as <a href="https://www.javascripttutorial.net/javascript-if-else/"><code>if-else</code></a> and <a href="https://www.javascripttutorial.net/javascript-switch-case/"><code>switch</code></a> statements. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> a = <span class="hljs-number">20</span>,
b = <span class="hljs-number">30</span>;
<span class="hljs-keyword">function</span> <span class="hljs-title function_">divide</span>(<span class="hljs-params">a, b</span>) {
<span class="hljs-keyword">if</span>(b == <span class="hljs-number">0</span>) {
<span class="hljs-keyword">throw</span> <span class="hljs-string">'Division by zero'</span>;
}
<span class="hljs-keyword">return</span> a / b;
}<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>In the <code>divide()</code> function, we check whether the de-numerator (b) is zero. If yes, we <a href="https://www.javascripttutorial.net/javascript-try-catch/">throw an exception</a>. Otherwise, we return the result of a / b.</p>
<p>To declare an <a href="https://www.javascripttutorial.net/javascript-array/">array</a>, you use the following syntax:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> items = [];<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>To declare an array with some initial elements, you specify the elements in the square brackets:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> items = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>You can access the number of elements in the <code>items</code> array through its <code>length</code> property:</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(items.<span class="hljs-property">length</span>); <span class="hljs-comment">// 3Code language: JavaScript (javascript)</span>
</code></pre>
<p>To iterate over the elements of the <code>items</code> array, you use the <a href="https://www.javascripttutorial.net/javascript-for-loop/"><code>for</code></a> loop statement as follows:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < items.<span class="hljs-property">length</span>; i++) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(items[i]);
}<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>Or use the <code>for...of</code> loop in <a href="https://www.javascripttutorial.net/es6/">ES6</a>:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> item <span class="hljs-keyword">of</span> items) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(item);
}<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>JavaScript is an evolving language. It has many other features that you’ll learn in the following tutorials.</p>
<p>In this tutorial, you learned what JavaScript is and the overview of the JavaScript language.</p>