✴️Learn JavaScript Tutorial
Our JavaScript Tutorial Is designed for beginners and professionals both. JavaScript is used to create client-side dynamic pages.
JavaScript is an object-based scripting language which is lightweight and cross-platform.
JavaScript is not a compiled language, but it is a translated language. The JavaScript Translator (embedded in the browser) is responsible for translating the JavaScript code for the web browser.
Our JavaScript Tutorial Is designed for beginners and professionals both. JavaScript is used to create client-side dynamic pages.
JavaScript is an object-based scripting language which is lightweight and cross-platform.
JavaScript is not a compiled language, but it is a translated language. The JavaScript Translator (embedded in the browser) is responsible for translating the JavaScript code for the web browser.
✴️Application of JavaScript
JavaScript is used to create interactive websites. It is mainly used for:
Client-side validation,
Dynamic drop-down menus,
Displaying date and time,
Displaying pop-up windows and dialog boxes (like an alert dialog box, confirm dialog box and prompt dialog box),
Displaying clocks etc.
JavaScript is used to create interactive websites. It is mainly used for:
Client-side validation,
Dynamic drop-down menus,
Displaying date and time,
Displaying pop-up windows and dialog boxes (like an alert dialog box, confirm dialog box and prompt dialog box),
Displaying clocks etc.
1. Program to print hello world
<html>
<body>
<script type="text/javascript">
document.write("hello World");
</script>
</body>
</html>
<html>
<body>
<script type="text/javascript">
document.write("hello World");
</script>
</body>
</html>
JavaScript Tutorials:
2. 2. Program to swap two strings.
<html>
<head>
<script> function swap()
{
var txt = document.getElementById('t1').value;
document.getElementById('t1').value = document.getElementById('t2').value; document.getElementById('t2').value = txt;
}
</script>
</head>
<body>
<input type="text" id="t1"> <input type="text" id="t2">
<br>
<input type="button" value="Swap" onclick="swap()">
</body>
</html>
2. 2. Program to swap two strings.
<html>
<head>
<script> function swap()
{
var txt = document.getElementById('t1').value;
document.getElementById('t1').value = document.getElementById('t2').value; document.getElementById('t2').value = txt;
}
</script>
</head>
<body>
<input type="text" id="t1"> <input type="text" id="t2">
<br>
<input type="button" value="Swap" onclick="swap()">
</body>
</html>
3. Program to add text to a particular division in the page.
<script>
function add1()
{
document.getElementById('div1').innerHTML = document.getElementById('t1').value
}
</script>
<input type="text" id="t1"> <br> <input type="button" value="Click" onclick="add1()"> <div id="div1">
</div>
<script>
function add1()
{
document.getElementById('div1').innerHTML = document.getElementById('t1').value
}
</script>
<input type="text" id="t1"> <br> <input type="button" value="Click" onclick="add1()"> <div id="div1">
</div>
4. Program to change style of a text at runtime.
<script>
function chngstyle()
{
document.getElementById('div1').style.color = "Red"
document.getElementById('div1').style.backgroundColor = "yellow"
document.getElementById('div1').style.width = "100"
document.getElementById('div1').style.height = "100"
}
</script>
<div id="div1"> Welcome to all </div>
<input type="button" value="Click" onclick="chngstyle()">
<script>
function chngstyle()
{
document.getElementById('div1').style.color = "Red"
document.getElementById('div1').style.backgroundColor = "yellow"
document.getElementById('div1').style.width = "100"
document.getElementById('div1').style.height = "100"
}
</script>
<div id="div1"> Welcome to all </div>
<input type="button" value="Click" onclick="chngstyle()">
6. Program to change color of the text box if empty string submitted.
<script>
function funup(val)
{
if(val.length>0) document.getElementById('t1').style.borderColor="silver";
}
function fun1()
{
txt=document.getElementById('t1').value if(txt.length == 0) document.getElementById('t1').style.borderColor = "red"
}
</script>
<input type="text" id="t1" onkeyup="funup(this.value)">
<input type="button" value="Submit" onclick="fun1()">
<script>
function funup(val)
{
if(val.length>0) document.getElementById('t1').style.borderColor="silver";
}
function fun1()
{
txt=document.getElementById('t1').value if(txt.length == 0) document.getElementById('t1').style.borderColor = "red"
}
</script>
<input type="text" id="t1" onkeyup="funup(this.value)">
<input type="button" value="Submit" onclick="fun1()">
7. Write a JavaScript program to display the current day and time in the following format.
<script>
var today = new Date();
var day = today.getDay();
var daylist=["Sunday"
,"Monday","Tuesday"
,"Wednesday","Thursday","Friday",
"Saturday"];
console.log("Today is : " + daylist[day] + ".");
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var prepand = (hour >= 12)? " PM ":" AM ";
hour = (hour >= 12)? hour - 12: hour;
if (hour===0 && prepand===' PM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Noon';
}
else
{
hour=12;
prepand=' PM';
}
}
if (hour===0 && prepand===' AM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Midnight';
}
else
{
hour=12;
prepand=' AM';
}
}
console.log("Current Time : "+hour + prepand + " : " + minute + " : " + second);
</script>
<script>
var today = new Date();
var day = today.getDay();
var daylist=["Sunday"
,"Monday","Tuesday"
,"Wednesday","Thursday","Friday",
"Saturday"];
console.log("Today is : " + daylist[day] + ".");
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var prepand = (hour >= 12)? " PM ":" AM ";
hour = (hour >= 12)? hour - 12: hour;
if (hour===0 && prepand===' PM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Noon';
}
else
{
hour=12;
prepand=' PM';
}
}
if (hour===0 && prepand===' AM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Midnight';
}
else
{
hour=12;
prepand=' AM';
}
}
console.log("Current Time : "+hour + prepand + " : " + minute + " : " + second);
</script>
8. Write a JavaScript program to get the current date.
<script>
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10)
{
dd='0'+dd;
}
if(mm<10)
{
mm='0'+mm;
}
today = mm+'-'+dd+'-'+yyyy;
console.log(today);
today = mm+'/'+dd+'/'+yyyy;
console.log(today);
today = dd+'-'+mm+'-'+yyyy;
console.log(today);
today = dd+'/'+mm+'/'+yyyy;
console.log(today);
</script>
<script>
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10)
{
dd='0'+dd;
}
if(mm<10)
{
mm='0'+mm;
}
today = mm+'-'+dd+'-'+yyyy;
console.log(today);
today = mm+'/'+dd+'/'+yyyy;
console.log(today);
today = dd+'-'+mm+'-'+yyyy;
console.log(today);
today = dd+'/'+mm+'/'+yyyy;
console.log(today);
</script>
9. Write a JavaScript function to find the area of a triangle where lengths of the three of its sides are 5, 6, 7.
<script>
var side1 = 5;
var side2 = 6;
var side3 = 7;
var s = (side1 + side2 + side3)/2;
var area = Math.sqrt(s*((s-side1)*(s-side2)*(s-side3)));
console.log(area);
</script>
<script>
var side1 = 5;
var side2 = 6;
var side3 = 7;
var s = (side1 + side2 + side3)/2;
var area = Math.sqrt(s*((s-side1)*(s-side2)*(s-side3)));
console.log(area);
</script>
10. Write a JavaScript program to determine whether a given year is a leap year in the Gregorian calendar.
<script>
year = window.prompt("Input a Year : ");
x = (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
console.log(x);
</script>
<script>
year = window.prompt("Input a Year : ");
x = (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
console.log(x);
</script>
