JavaScript Tools and Basics
1. JavaScript Tools
JavaScript tools are used to write, run, test, and debug JavaScript code.
Common JavaScript Tools:
1. Web Browser
Browsers like Google Chrome, Firefox, and Edge support JavaScript execution.
JavaScript runs inside the browser using a JavaScript Engine.
Chrome uses V8 Engine.
Firefox uses SpiderMonkey.
2. Code Editors
Used to write JavaScript code.
Examples: Visual Studio Code, Sublime Text, Notepad++.
3. Browser Developer Tools
Used to test and debug JavaScript.
Open using F12 or Right Click → Inspect.
Console tab is mainly used for JavaScript.
Example:
[Link]("Welcome to JavaScript");
4. Online JavaScript Editors
JSFiddle, CodePen, W3Schools Tryit Editor.
2. Introduction to JavaScript
JavaScript is a client-side scripting language.
It is used to make web pages interactive and dynamic.
JavaScript can accept user input, perform calculations, make decisions, and validate forms.
3. User Input with Prompt Dialogs
Prompt Dialog is used to get input from the user.
It displays a dialog box with a text field.
Syntax:
prompt("message");
Example:
let name = prompt("Enter your name:");
[Link]("Welcome " + name);
Prompt with number:
let age = prompt("Enter your age:");
[Link]("Your age is " + age);
Note: Data entered through prompt() is always treated as string.
4. JavaScript Data Types
1. Number:
Used for numeric values.
Example:
let a = 10;
let b = 20.5;
2. String:
Used for text.
Written inside quotes.
Example:
let name = "Mohana";
3. Boolean:
Holds true or false.
Example:
let isPassed = true;
4. Undefined:
Variable declared but not assigned.
Example:
let x;
5. Null:
Represents no value.
Example:
let y = null;
Checking Data Type:
typeof(variable);
Example:
let a = 10;
[Link](typeof(a));
5. Arithmetic Operations in JavaScript
Arithmetic Operators:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Example Program:
let a = 10;
let b = 5;
[Link](a+b);
[Link](a-b);
[Link](a*b);
[Link](a/b);
[Link](a%b);
Arithmetic with User Input:
let x = Number(prompt("Enter first number"));
let y = Number(prompt("Enter second number"));
let sum = x + y;
[Link]("Sum = " + sum);
6. Decision Making in JavaScript
1. if Statement
Syntax:
if(condition){
statement;
Example:
let age = 18;
if(age>=18){
[Link]("Eligible to vote");
2. if else Statement
Syntax:
if(condition){
statement1;
}
else{
statement2;
Example:
let mark = 45;
if(mark>=50){
[Link]("Pass");
else{
[Link]("Fail");
3. else if Ladder
Syntax:
if(condition1){
statement1;
else if(condition2){
statement2;
else{
statement3;
}
Example:
let marks = 85;
if(marks>=90){
[Link]("Grade A");
else if(marks>=75){
[Link]("Grade B");
else if(marks>=50){
[Link]("Grade C");
else{
[Link]("Fail");
4. switch Statement
Syntax:
switch(expression){
case value1:
statement;
break;
case value2:
statement;
break;
default:
statement;
}
Example:
let day = 3;
switch(day){
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
default:
[Link]("Invalid Day");