Java Script Fundamentals - 2
1. console.log()
The console.log()
method is used to print output to the browser’s console (or in the Node.js environment, to the terminal). It is useful for debugging purposes by displaying the value of variables, objects, or expressions.
Other variants in this are:
2. Template Literals
Template literals allow you to include expressions in a string and format it easily without the need for concatenation. They are enclosed by backticks (`
) and allow embedding variables using ${}
3. Comparison Operators
Comparison operators compare two values and return a Boolean (true
or false
).
For Numbers:
==
: Equal to (compares values, ignores type )
===
: Strict equal to (compares values and types)
!=
: Not equal to (compares values, ignores type)
!==
: Strict not equal to (compares values and types)
>
: Greater than>=
: Greater than or equal to<
: Less than<=
: Less than or equal to
For Non-Numbers:
JavaScript uses a lexicographical (alphabetical) comparison for strings:
For objects, it compares references:
4. Conditional Statement
Conditional statements are used to perform different actions based on different conditions.
if
StatementThe
if
statement executes a block of code if a specified condition istrue
.
else if
StatementThe
else if
statement adds another condition to anif
statement. If the first condition is false, it checks the next one.
Nested
if-else
You can nest
if
statements inside otherif
orelse
blocks.
Switch Statement
The
switch
statement is used to execute one block of code among many options
5. Logical Operator
Logical operators are used to combine conditions in if
statements.
&&
(AND): Returnstrue
if both conditions are true
||
(OR): Returnstrue
if at least one condition is true
!
(NOT): Inverts the value of a boolean
6. Truthy And Falsy
In JavaScript, some values are inherently truthy
or falsy
in conditional statements.
Falsy values:
false
,0
,""
(empty string),null
,undefined
,NaN
Truthy values: All other values, including non-empty strings, objects, arrays, and
true
7. Alerts And Prompts
alert()
: Displays a message to the user in a dialog box
prompt()
: Displays a dialog box with a text input field. The user’s input is returned