0% found this document useful (0 votes)
24 views8 pages

02 - Variable and Datatypes

The document provides an overview of JavaScript basics, including the use of console.log, comments, variables, and data types. It explains the three stages of a variable (declaration, initialization, and use) and the differences between var, let, and const. Additionally, it covers primitive data types, variable naming rules, and includes a series of questions related to JavaScript concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

02 - Variable and Datatypes

The document provides an overview of JavaScript basics, including the use of console.log, comments, variables, and data types. It explains the three stages of a variable (declaration, initialization, and use) and the differences between var, let, and const. Additionally, it covers primitive data types, variable naming rules, and includes a series of questions related to JavaScript concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Console in JavaScript

[Link] is used to print/log a message/output to the console.

Comments in JavaScript
Part of code which are not going to execute, and just for reference or good readability of code.

Two Types of Comment:


Variables:
A variable is like a container that holds data. It is used to store data that can be used and
manipulated throughout your program. .

Think of a variable as a named container that holds a value.

a b box1 box2

In JavaScript, a variable can contain any type of data.

23 45.34 A Manas
a b box1 box2

Three Stages of a Variable:


1. Declaration
2. Initialization
3. Use
1. Declaration Examples:

2. Initialization
Here “=“ is an
assignment operator
3. Use

Declaration + Initialization:
Let, const & var
var : Variable can be re-declared & updated. A global scope variable.

let : Variable cannot be re-declared but can be updated. A block scope variable.

const : Variable cannot be re-declared or updated. A block scope variable.


(variables declared with const keyword must have declaration & initialization both)

Rules for variable declaration:


1. Variable names are case sensitive;

2. “a” & “A” is different.

3. Only letters, digits, underscore( _ ) and $ is allowed. (not even space)

4. Only a letter, underscore( _ ) or $ should be 1st character.

5. Reserved words cannot be variable names.

Primary (Primitive) Datatypes:


❑ Number, String, Boolean, Undefined, Null, BigInt, Symbol
❑ Use typeof operator to identify the type of variable

Different Types of cases exists in Programming:


❑ camelCase, snake_case, PascalCase, and kebab-case
Use Case 1:
Suppose price of two products comes from database, you need to store these prices and show the total as output.

Use Case 2:
A user enters their first and last name into a form, and you need to display the full name.
JavaScript is a Dynamically Typed Language & Forgiving Language

You don’t need to specify the type of a variable,


even variable dynamically change its type respective to the data assigned in it.

Forgive and does not produce any error


Q1: How do you declare a variable in JavaScript?

Q2: What is the difference between var, let, and const?

Q3: Can you change the value of a const variable?

Q4: What will happen if you use a variable without declaring it?

Q5: What is the default value of an uninitialized variable in JavaScript?

Q6: What are the primitive data types in JavaScript?

Q7: What is the difference between null and undefined?

Q8: Is JavaScript a statically typed or dynamically typed language?

Q9: What will be the output of typeof null?

Q10: What happens when you add a number and a string in JavaScript?

Q11: What is type coercion? Give an example.

Q12: How can you manually convert a string to a number in JavaScript?

Q13: What is the result of "5" - 3 in JavaScript?

Q14: What is NaN in JavaScript, and how do you check if a value is NaN?

Q15: How do you check the type of a variable in JavaScript?

You might also like