JavaScript is the most popular programming language in the world.
56 | JavaScript is the programming language of HTML and the Web.
57 | Programming makes computers do what you want them to do.
58 | JavaScript is easy to learn.
59 |
java script can change html content
60 |
java script can change html Attribute
61 |
java script can change html Style CSS
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
2
73 |
74 |
75 |
Note:please remove the space from closing tag(body) at bottom of examples.
76 |
The <"script"> Tag
77 |
In HTML, JavaScript code must be inserted between <"script"> and <"/script"> tags.
78 |
79 | Example:=:
80 |
81 |
82 |
83 |
86 |
87 |
88 |
89 |
JavaScript in <"head">
90 |
In this example, a JavaScript function is placed in the <"head"> section of an HTML page.
91 | The function is invoked (called) when a button is clicked:
In this example, a JavaScript function is placed in the <"body"> section of an HTML page.
120 | The function is invoked (called) when a button is clicked:
Scripts can also be placed in external files.
149 | External scripts are practical when the same
150 | code is used in many different web pages.
151 | JavaScript files have the file extension .js.
152 | To use an external script, put the name of the script
153 | file in the src (source) attribute of the <"script"> tag:
To access an HTML element, JavaScript can use the document.getElementById(id) method.
248 | The id attribute defines the HTML element. The innerHTML property defines the HTML content:
Note:please remove the space from closing tag(body) at bottom of examples.
301 |
302 |
JavaScript Syntax
303 |
JavaScript Programs
304 |
A computer program is a list of "instructions" to be "executed" by the computer.
305 | In a programming language, these program instructions are called statements.
306 | JavaScript is a programming language.
307 | JavaScript statements are separated by semicolons.
308 |
309 | Example:
310 |
311 | var x = 5;
312 | var y = 6;
313 | var z = x + y;
314 |
315 |
316 |
317 |
JavaScript Statements
318 |
JavaScript statements are composed of:
319 | Values, Operators, Expressions, Keywords, and Comments.
320 |
321 |
322 |
JavaScript Values
323 |
The JavaScript syntax defines two types of values: Fixed values and variable values.
324 | Fixed values are called literals. Variable values are called variables.
325 |
326 |
327 |
JavaScript Literals
328 |
The most important rules for writing fixed values are:
329 | Numbers are written with or without decimals:
330 | example of numbers:
331 | 7,14,12
332 |
333 |
334 | Strings are text, written within double or single quotes:
335 | Example of String
336 | "Sultan"
337 | "Muhammad"
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
5
348 |
Note:please remove the space from closing tag(body) at bottom of examples.
349 |
350 |
351 |
JavaScript Comments
352 |
avaScript comments can be used to explain JavaScript code, and to make it more readable.
353 | JavaScript comments can also be used to prevent execution, when testing alternative code.
354 |
355 |
356 |
357 |
Single Line Comments
358 |
Single line comments start with //.
359 | Any text between // and the end of the line, will be ignored by JavaScript (will not be executed).
360 | This example uses a single line comment before each line, to explain the code:
361 |
362 | Example:
363 |
364 | // Change heading:
365 | document.getElementById("myH").innerHTML = "My First Page";
366 | // Change paragraph:
367 | document.getElementById("myP").innerHTML = "My first paragraph.";
368 |
369 |
370 |
371 |
372 |
373 |
374 |
Multi-line Comments
375 |
Multi-line comments start with /* and end with */.
376 | Any text between /* and */ will be ignored by JavaScript.
377 | This example uses a multi-line comment (a comment block) to explain the code:
378 | Example:
379 |
380 | /*
381 | The code below will change
382 | the heading with id = "myH"
383 | and the paragraph with id = "myP"
384 | in my web page:
385 | */
386 | document.getElementById("myH").innerHTML = "My First Page";
387 | document.getElementById("myP").innerHTML = "My first paragraph.";
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
6
397 |
Note:please remove the space from closing tag(body) at bottom of examples.
398 |
399 |
400 |
JavaScript Variables
401 |
402 | JavaScript variables are containers for storing data values.
403 | In this example, x, y, and z, are variables:
404 | There are theee type of variable such as:
Let
Const
Var ::ab sa phala wala variable(var ha)
405 |
406 | Example:
407 |
408 | var x = 5;
409 | var y = 6;
410 | var z = x + y;
411 |
412 |
413 | From the example above, you can expect:
414 |
x stores the value 5
415 |
y stores the value 6
416 |
z stores the value 11
417 |
418 |
419 |
420 |
421 |
Data Types
422 |
There are two types of data types such as
423 |
primitive data type
non primitive data type
424 |
425 |
426 |
Primitive Data Type
427 |
there are five types of primitive data type Such as
428 |
String
429 |
Number
430 |
Booleans
431 |
undefine
432 |
null
433 |
434 |
435 |
436 |
437 |
String
438 |
A string can be any text inside double or single quotes like "sultan" sultan is a string
439 | Example of String:
440 |
"Sultan"
441 |
"Muhammad"
442 |
"Essa"
443 |
444 |
445 |
446 |
Number
447 |
448 | Example:
449 |
1
450 |
51
451 |
7
452 |
453 |
454 |
455 |
Booleans
456 |
Boleans which basically means they can be true or false ,on or off and close or open etc .
457 |
True
458 |
False
459 |
460 |
461 |
462 |
463 |
undefine
464 |
A variable that has not been assigned a value
465 | Example::
466 |
let a;
467 |
468 |
469 |
470 |
Null
471 |
'Null' is a keyword in JavaScript that signifies 'no value' or nonexistence of any value.
472 | Example:
473 | let a=null;
474 |
475 |
476 |
477 |
478 |
479 |
Primitive Data Type
480 |
Such a data which is not easily changed, means data is changed in difficult way
481 | There are two tyes of primitive data type
482 |
Array
483 |
Object
484 |
485 |
486 |
487 |
488 |
Array
489 |
An array is a special variable, which can hold more than one value at a time. The Syntax of array is different from object An array is start from square brackets.
490 | Example:
491 |
492 | let arry(variable name which u want)=["Sultan","Muhammad","Essa"]
493 |
494 | Note:for more detail please visit portion.
495 |
496 |
497 |
498 |
499 |
500 |
501 |
Object
502 |
An object is a special variable which can hold more than one value at a time.The syntax of array is different from array object is start with curly brackets
Adding two numbers, will return the sum, but adding a number and a string will return a string:
743 | Example:
744 | x = 2 + 5;
745 | y = "5" + 5;
746 | z = "Sultan" + 5;
747 |
748 | The result of x, y, and z will be:
749 | 7
750 | 55
751 | Sultan
752 |
753 |
754 |
755 |
JavaScript Comparison and Logical Operators
756 |
757 |
758 |
759 |
Operators
760 |
Description
761 |
762 |
763 |
764 |
==
765 |
equal to
766 |
767 |
768 |
769 |
===
770 |
equal value and equal type
771 |
772 |
773 |
774 |
!=
775 |
not equal
776 |
777 |
778 |
779 |
!==
780 |
Not equal value or not equal type
781 |
782 |
783 |
784 |
>
785 |
Greater Than
786 |
787 |
788 |
789 |
<
790 |
Less than
791 |
792 |
793 |
794 |
>=
795 |
greater than or equal to
796 |
797 |
798 |
799 |
<
800 |
Less than or equal to
801 |
802 |
803 |
804 |
?
805 |
Ternatry operator
806 |
807 |
808 |
809 |
810 |
811 |
812 |
813 |
814 |
815 |
816 |
8
817 |
Note:please remove the space from closing tag(body) at bottom of examples.
818 |
819 |
JavaScript Arithmetic
820 |
JavaScript Arithmetic Operators
821 |
822 |
Arithmetic operators perform arithmetic on numbers (literals or variables).
823 |
824 |
825 |
826 |
827 |
Operator
828 |
Description
829 |
830 |
831 |
832 |
+
833 |
Addition
834 |
835 |
836 |
837 |
-
838 |
Subtraction
839 |
840 |
841 |
842 |
*
843 |
Multiplication
844 |
845 |
846 |
847 |
/
848 |
Division
849 |
850 |
851 |
852 |
%
853 |
Modulus
854 |
855 |
856 |
857 |
++
858 |
Increment
859 |
860 |
861 |
862 |
--
863 |
Decrement
864 |
865 |
866 |
867 |
868 |
869 |
870 |
871 |
Operators and Operands
872 |
The numbers (in an arithmetic operation) are called operands.
873 | The operation (to be performed between the two operands) is defined by an operator.
874 |
875 |
876 |
877 |
878 |
operand
879 |
operator
880 |
operand
881 |
882 |
883 |
884 |
12
885 |
*
886 |
14
887 |
888 |
889 |
890 |
891 |
892 |
893 |
894 |
Adding operator
895 |
The addition operator (+) adds numbers:
896 | Example:
897 | var x = 5;
898 | var y = 2;
899 | var z = x + y;
900 |
901 |
902 |
Subtraction operator
903 |
The subtraction operator (-) subtracts numbers.
904 | Example:
905 | var x = 5;
906 | var y = 2;
907 | var z = x - y;
908 |
909 |
910 |
911 |
Multiplication operator
912 |
The multiplication operator (*) multiplies numbers.
913 | Example:
914 | var x = 5;
915 | var y = 2;
916 | var z = x * y;
917 |
918 |
919 |
Division operator
920 |
The division operator (/) divides numbers.
921 | Example:
922 | var x = 5;
923 | var y = 2;
924 | var z = x / y;
925 |
926 |
927 |
928 |
Modular operator
929 |
The modular operator (%) returns the division remainder.
930 | Example:
931 | var x = 5;
932 | var y = 2;
933 | var z = x % y;
934 |
935 |
936 |
937 |
938 |
Increment operator
939 |
The increment operator (++) increments numbers.
940 | Example:
941 | var x = 5;
942 | x++;
943 | var z = x;
944 |
945 |
946 |
Decrement operator
947 |
The decrement operator (--) decrements numbers.
948 | Example:
949 | var x = 5;
950 | x--;
951 | var z = x;
952 |
953 |
954 |
955 |
956 |
957 |
9
958 |
Note:please remove the space from closing tag(body) at bottom of examples.
959 |
960 |
961 |
JavaScript Assignment Operators
962 |
Assignment operators assign values to JavaScript variables.
963 |
964 |
965 |
966 |
967 |
968 |
Operator
969 |
Example
970 |
Same As
971 |
972 |
973 |
974 |
=
975 |
x=y
976 |
x=y
977 |
978 |
979 |
980 |
+=
981 |
x+=y
982 |
x=x+y
983 |
984 |
985 |
986 |
987 |
-=
988 |
x-=y
989 |
x=x-y
990 |
991 |
992 |
993 |
*=
994 |
x*=y
995 |
x=x*y
996 |
997 |
998 |
999 |
/=
1000 |
x/y
1001 |
x=x/y
1002 |
1003 |
1004 |
1005 |
%=
1006 |
x%y
1007 |
x=x%y
1008 |
1009 |
1010 |
1011 |
1012 |
1013 |
= Assignment operator
1014 |
The = assignment operator assigns a value to a variable.
1015 | Example:
1016 | var a=10;
1017 | let b=5;
1018 |
1019 |
1020 |
+= Assignment operator
1021 |
The += assignment operator adds a value to a variable.
1022 | Example:
1023 | var x=10;
1024 | x+=5;
1025 |
1026 |
1027 |
-= Assignment operator
1028 |
The -= assignment operator subtracts a value from a variable.