---
description: This section contains some basic usage of JavaScript along with some examples.
---

# JavaScript

JavaScript is used in millions of Web pages to improve the design, validate forms, detect browsers, create cookies, and much more. JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer, Mozilla, Firefox, Netscape, Opera. Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called the JavaScript engine.

* JavaScript was designed to add interactivity to HTML pages&#x20;
* JavaScript is a scripting language (a scripting language is a lightweight programming language)&#x20;
* A JavaScript consists of lines of executable computer code&#x20;
* A JavaScript is usually embedded directly into HTML pages&#x20;
* JavaScript is an interpreted language (means that scripts execute without preliminary compilation)&#x20;
* Everyone can use JavaScript without purchasing a license

**Are Java and JavaScript the Same? -** NO! Java and JavaScript are two completely different languages in both concept and design! Java (developed by Sun Microsystems) is a powerful and much more complex programming language -in the same category as C and C++.

#### Sample JavaScript

```javascript
<html>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
```

#### **JavaScript Engine**&#x20;

* The browser has an embedded engine sometimes called a “JavaScript virtual machine”.
* Different engines have different “codenames”. For example:
  * V8 –in Chrome and Opera.
  * Spider Monkey –in Firefox.
* There are other codenames like “Trident” and “Chakra” for different versions of IE, “ChakraCore” for Microsoft Edge, “Nitro” and “SquirrelFish” for Safari, etc.
* The terms above are good to remember because they are used in developer articles on the internet. We’ll use them too. For instance, if “a feature X is supported by V8”, then it probably works in Chrome and Opera

#### Ending Statements With a Semicolon?

* With traditional programming languages, like C++ and Java, each code statement has to end with a semicolon (;).
* Many programmers continue this habit when writing JavaScript, but in general, semicolons are optional!&#x20;
* However, semicolons are required if you want to put more than one statement on a single line.

#### JavaScript Variables

* Variables are used to store data.&#x20;
* A variable is a "container" for the information you want to store.
* A variable's value can change during the script.&#x20;
* You can refer to a variable by name to see its value or to change its value.
* Rules for variable names:
  * Variable names are case sensitive&#x20;
  * They must begin with a letter or the underscore character •
    * strname –STRNAME (not same)

#### Arithmetic Operations

![](https://3903980405-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_kux1uuaXhHbpyCSYu%2F-MbbMOtYjn-5Q0XMgSvr%2F-MbbPFfuwNaMdnmUxbOc%2Fimage.png?alt=media\&token=8c804285-b313-449c-8901-65cd053da284)

#### Assignment Operations

![](https://3903980405-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_kux1uuaXhHbpyCSYu%2F-MbbMOtYjn-5Q0XMgSvr%2F-MbbPLtG7de6zxDsq2lq%2Fimage.png?alt=media\&token=c15b0658-8cbc-4c12-b4aa-48beea83dad9)

#### Comparison Operators

![](https://3903980405-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_kux1uuaXhHbpyCSYu%2F-MbbMOtYjn-5Q0XMgSvr%2F-MbbPX-ZoR4cTjJEhVaY%2Fimage.png?alt=media\&token=d00192e5-3360-4353-a2be-563abd031407)

#### Logical Operations

![](https://3903980405-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_kux1uuaXhHbpyCSYu%2F-MbbMOtYjn-5Q0XMgSvr%2F-MbbPgKNkxN8GheA-7sX%2Fimage.png?alt=media\&token=4e384477-41ef-4c17-9602-32080035ef84)

#### JavaScript Pop-Up Boxes

**Alert Box** - An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click "OK" to proceed.

```javascript
<script>alert("Hello World!")</script>
```

**Confirm Box** - A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

```javascript
<html>
<head> 
<title>JS Confirm Box</title>
</head>
<body>
<p>Click to display a confirm box.</p>
<button onclick="myFunction()">Click Here</button>
<script>
function myFunction() 
{
confirm("Press the button!");
}
</script>
</body>
</html>
```

**Prompt Box** - A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK“, the box returns the input value. If the user clicks "Cancel“, the box returns null.

```javascript
<script>
x=prompt("Is this a Prompt Box  :", " ")
document.write("This is a Prompt Box :"+x)
</script>
```

**Conditional Statements -** Example for If and Else

```javascript
<script>
x=3
if(x<0)
{
alert (“Negative”)
}
else
{
alert (“Positive”)
}
</script>
```

**Additional Resources:**

1. Mozilla Resources - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference)
2. All about JavaScript - [https://javascript.info/](https://javascript.info/)

