- JavaScript is the Programming Language of Browser.
- It gives life to web pages.
- Client-Side Scripting Language.
- Used along with HTML
- JavaScript is Everywhere
- Developer choice*
- JavaScript easy to learn
- JavaScript can change HTML content
*Developer choice
https://insights.stackoverflow.com/survey/2019#technology-_-programming-scripting-and-markup-languages
- JavaScript is can be executed on the server (node.js)
- Android/ios developed (react native)
JavaScript embed location
- head
- body
- body footer
It’s will better if we load js in before the end of the body.
- With creating a JS file/ external file load (cache advantage – best way)
- Embed in HTML file with script tag – in header or footer area
- In HTML tag attributes – basically even related
4 Types output Method in WordPress
- window.alert(); / alert();
- document.write();
- innerHTML
- console.log();
Live JavaScript 4 Output Method Code
<script> window.alert('Your content'); alert('Your content'); // this two thing working same document.write('Your content'); // we can rewrite total document with this function - rewrite can be check by * // use for testing only document.getElementById('html-tag-id').innerHTML = "Your content"; // we can push content, or modify html content console.log('Your content'); // we can show output in browser console // use for debugging </script>
Rewrite with document.write
document.addEventListener("DOMContentLoaded", function(){ //it's will be load after full document loaded once document.write('Your content'); });
Output access method
- window.print()
- print()
<!-- Both are for printing --> <button id="abc" onclick="window.print()">Print</button> <button id="abc" onclick="print()">Print</button>
- JavaScript Ignore Multiple Space
- JavaScript case sensitive
Some Quick Point
- Statement – Statement is the single line of instruction in a program.
- JavaScript can not count more than one space.
JavaScript Comment
- A single line can be commented by / (slash)
- Multiple lines can be commented by this format /* multiple lines */
User Input Function
prompt(‘instruction’)
Variable in JavaScript
What is variable ?
Variable is used storing data in a programming language.
We can define a variable in three different ways in JS. This is:
- var
- let
- const
With this 3 ways, we can declare a variable.
var example
var a = 10 ;
var b;
var b = 10;
let example
let was came in ES6
let can’t be redeclared
example:
<script> let a = 5; let a = 10; document.write(a); </script> //uncaught SyntaxError: Identifier 'a' has already been declared
let have block scope facility
<script> if(true){ let x = 5; console.log(x);//it's will show output } console.log(x);// it's will show a error //cause it's in different block </script>
let can not be hoisted
const example
const can not be redeclared and reassigned, it’s also not possible to declare const variable without defining it.
<script> const a = 5; //const a - wrong //const a; a = 5; - wrong </script>
JavaScript Operator
There are few type operator in JavaScript
- Assignment operators
- Comparison operators
- Arithmetic operators
- Bitwise operators
- Logical operators
- String operators
- Conditional (ternary) operator
- Comma operator
- Unary operators
- Relational operators
jQuery Syntax for WordPress
(function($){ }) (jQuery)