What is JavaScript?
JavaScript is a programming language used to make web pages interactive. It runs on your visitor's computer and doesn't require constant downloads from your website. JavaScript is often used to create polls and quizzes. It basically programs the behaviour of web pages.
JavaScript and Java are not the same
They are two completely different computer languages. Only their names are similar.
Is JavaScript case sensitive?
Yes, JavaScript is case sensitive programming language. Variable names, keywords, methods, object properties and event handlers all are case sensitive.
Correct Example: <script>
alert("JavaScript Basics");
</script>
Incorrect Example: <script>
Alert("JavaScript Basics");
</script>
Correct Example: <script>
alert("JavaScript Basics");
</script>
Incorrect Example: <script>
Alert("JavaScript Basics");
</script>
Comments in JavaScript
- Single Line Comment
// This is a single line comment
</script>
2. Multi Line Comment
<script>
/* This is a multi line comment */
</script>
Data Types in JavaScript
- Numbers - 5, 5.234
- Boolean - true/false
- String - "MyString", 'MyString'
Variables
To create any variable in JavaScript use var keyword. Variable names are case sensitive.
JavaScript: Dynamically typed language
This means JavaScript data types are converted automatically as needed during script execution. Novice that, in myVariable we are first storing a number and then a string later.
<script>
var myVariable = 100;
alert(myVariable);
myVariable = "Assigning a string value";
alert(myVariable);
</script>
<script>
var myVariable = 100;
alert(myVariable);
myVariable = "Assigning a string value";
alert(myVariable);
</script>
Some other JavaScript Facts
- Inside which HTML element do we put the JavaScript? <script>
- Where is the correct place to insert a JavaScript? The <body> section
- What is the correct syntax for referring to an external script called "xxx.js"? <script src="xxx.js">
- What is the correct way to write a JavaScript array? var colors = ["red", "green", "blue"]
- What is the correct JavaScript syntax for opening a new window called "w2" ? w2 = window.open("http://www.w3schools.com");
- Which event occurs when the user clicks on an HTML element? onclick
- How do you write "Hello World" in an alert box? alert("Hello World");
- How do you create a function in JavaScript? function myFunction()
- How do you call a function named "myFunction"? myFunction()