π₯ JAVASCRIPT
JavaScript is a dynamic computer programming language.
It is lightweight and most commonly used as a part of web pages, whose
implementations allow client-side script to interact with the user and
make dynamic pages. It is an interpreted programming language with object-oriented capabilities.
π₯why use JavaScript
JavaScript is one of the 3 languages all web developers
must
learn:
1. HTML to define the content of web pages
2. CSS to specify the layout of web pages
3. JavaScript to program the behavior of web pages
π₯Where to use Java Script
π’ In HTML, JavaScript code must be inserted between <script> and </script> tags.
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
π’Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both.
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>
<p id="demo"></p>
<button type="button" onclick="myFunction()">Try
it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</body>
π’ Scripts can also be placed in external files
external files has some advantages:
- It separates HTML and code
- It makes HTML and JavaScript easier to read and maintain
- Cached JavaScript files can speed up page loads
<script src="myScript1.js"></script>
<script src="myScript2.js"></script>
π₯JavaScript can "display" data in different ways
- Writing into an HTML element, using innerHTML.
- Writing into the HTML output using document.write().
- Writing into an alert box, using window.alert().
- Writing into the browser console, using console.log().
π₯JavaScript syntax is the set of rules, how
JavaScript programs are constructed.
π₯JavaScript statements are composed of:
Values, Operators, Expressions, Keywords,
and Comments.
π₯The JavaScript syntax defines two types of values: Fixed values and variable
values.
Fixed values are called literals. Variable values are called
variables.
π₯ Java Script Variables
variables
are used to store data values.
JavaScript uses the var keyword to declare variables.
An equal sign is used to assign values to variables.
Eg:
<script>
var x;
x = 6;
document.getElementById("demo").innerHTML = x;
</script>
π’ The values can be of various types, such as numbers and strings.
Eg: "John" + " " + "Doe", evaluates to "John Doe"
π₯Java Script Comments
Code after double slashes
// or between /* and */ is treated as a comment
π₯JavaScript is Case Sensitive
All JavaScript identifiers are
case sensitive.
The variables lastName and lastname,
are two different variables.
Eg:
<script>
var lastname, lastName;
lastName = "Doe";
lastname = "Peterson";
document.getElementById("demo").innerHTML = lastName;
</script>
out put: Doe
π₯Java Script is Camel Case
Hyphens
first-name, last-name, master-card, inter-city.
π± Hyphens are not allowed in JavaScript. It is reserved for subtractions.
Underscore
first_name, last_name, master_card, inter_city.
Upper Camel Case
FirstName, LastName, MasterCard, InterCity.
Lower Camel Case
use camel case that starts with a lowercase letter
firstName, lastName, masterCard, interCity.