Day 6 - JavaScript Fundamentals
15 January, 2024
33
33
0
Contributors
- Day 6 - JavaScript Fundamentals
- Add JavaScript to html
- Similar to adding external css to a html page, we can add using
<script src="script.js">
element to add external js to the html page. - or writing all the javascript code with in the opening & closing tags of
<script>
- To add external css we use
<link>
element within the<head>
element in html where as the<script>
is added to the<body>
element since only metadata stays in the<head>
of the html file.
- Similar to adding external css to a html page, we can add using
- 8 Data types in JavaScript - mdn docs, javascript.info link
- Number
- BigInt
- String
- Boolean
- null
- In JavaScript,
null
is not a “reference to a non-existing object” or a “null pointer” like in some other languages. - It’s just a special value which represents “nothing”, “empty” or “value unknown”.
- In JavaScript,
- undefined
- Symbol
- Object
- Object is the only reference type and the rest are considered primitive data types.
- All operators in JavaScript including the assignment operator (
=
) returns a value 🙂. - Example
let a = 1;
let b = 2;
let c = 3 - (a = b + 1);
alert( a ); // 3
alert( c ); // 0
- Example
- Escape characters - https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Strings#including_quotes_in_strings:~:text=Escaping characters means
- For the single or double quotes to be considered within another string we use a
\\
right before those quotes or other special characters - This is to escape that character.const bigmouth = 'I\\'ve got no right to take my place…';
console.log(bigmouth);
// output: I've got no right to take my place…
- For the single or double quotes to be considered within another string we use a
+
is also used for concatenating the strings- since
+
is a binary operator & javascript goes from left to right for evaluating, it converts it to string if a either of the operand is a string else converts to num - Example
console.log(1 + "2" + 3);
// output: 123
console.log(2 + 3 + "8");
// output: 58
- since
- All non-numbers are tried to be converted to numbers when arithmetic operations are done in js, with some exceptions to
+
- Example
console.log(2 + 3 - "8");
// output: -3
- Example
- Declaration in js returns
undefined
let a;
→ returns undefined- Also when a declaration combined with assignment would also return
undefined
undefined
andnull
have a weird relation- the non-strict comparison (
==
) returnstrue
- and the strict comparison (
===
) since checks the type returnsfalse
null
is never equal to anything except as mentioned above
- the non-strict comparison (