Unit-2
This chapter introduces JavaScript as the programming language used to create interactive web applications. It explains client-side and server-side JavaScript, setting up the development environment, using browser developer tools, embedding JavaScript into HTML, writing readable code, taking user input, generating random numbers, and completing a small project.
JavaScript is a programming language that runs on both the client side (browser) and server side (Node.js). Modern interactive websites depend heavily on JavaScript.
Runs in the user's browser and provides immediate feedback.
Runs on remote servers handling business logic and databases.
Popular editors: Visual Studio Code, Atom, Sublime Text, WebStorm.
Chrome and Firefox are recommended.
node -v
npm -v
If npm execution policy error occurs:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
// log.js
console.log("Hello III CSE-A, Enjoy Java Script");
node log.js
| Technology | Purpose |
|---|---|
| HTML | Structure |
| CSS | Presentation |
| JavaScript | Behavior |
<html>
<body>
Hello World!
</body>
</html>
ECMAScript (ES6) is the standard specification for JavaScript.
console.log("Hello world!");
console.error("Error");
console.table(data);
Developer tools can usually be opened using F12.
<script>
alert("Hi there!");
</script>
<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
</head>
<body>
Content
<script>
console.log("Hi there!");
</script>
</body>
</html>
<script src="app.js"></script>
let status="new";
let scared=true;
if(status==="new"){
console.log("Welcome to JavaScript!");
if(scared){
console.log("Don't worry.");
}else{
console.log("You're brave!");
}
}else{
console.log("Welcome back!");
}
// Single-line comment
/*
Multi-line
comment
*/
Always use proper indentation and terminate statements using semicolons.
let response = prompt("Hi! How are you?");
console.log(response);
console.log(Math.random());
console.log(Math.random()*100);
console.log(Math.floor(Math.random()*100));
Math.random() returns values between 0 (inclusive) and 1 (exclusive).
<!DOCTYPE html>
<html>
<head>
<title>Chapter Project</title>
</head>
<body>
<h1>My First JavaScript Project</h1>
<script src="app.js"></script>
</body>
</html>
/*
This script logs the author's name.
*/
console.log("Your Name Here");
| Feature | Browser | Node.js |
|---|---|---|
| prompt() | Built-in | Not available |
| prompt-sync | Not supported | Supported |
| require() | Not available | Available |
| console.log() | Browser Console | Terminal |