Primitive Types & Conditionals

 

1. JavaScript Values

Basically In JavaScript values are of two categories.

  • Primitive Types
  • Reference Types

1.1 Primitive Types

  • Number
  • Boolean
  • String
  • Undefined, etc.
Primitive TypeDescription
NumberAll the numbers are of Number type.
BooleanBoolean values are either true or false.
StringString is a stream of characters. The String should be enclosed with Single quotes, Double quotes, or Backticks.
UndefinedIf a value is not assigned to the variable, then it takes 
undefined
 as its value. In JS, 
undefined
 refers to the value that is not being assigned.

1.2 Operators

1.2.1 typeof()

The

typeof()
operator is used to find the type of value.

let a = 900;
let b = 9.2;
console.log(typeof(a)); // number
console.log(typeof(b)); // number
JAVASCRIPT

Try out changing the different Values in the below Code Playground and check the output in the console.

let isApproved = false; console.log(typeof(isApproved)); console.log(typeof(true));

2. Converting String to a Number

In JavaScript, when we combine the number and string, it results in a string.

The

parseInt()

function accepts a string and converts it into an integer.

let a = '20';
console.log(typeof(a)); // string
let b = parseInt(a);
console.log(typeof(b)); // number
JAVASCRIPT

3. Conditional Statements

The Conditional Statement allows you to execute a block of code only when a specific condition is true.

If...Else Statement:

Syntax:

if (conditionA) {
Block1;
}
else if (conditionB) {
Block2;
}
else {
Block3;
}
JAVASCRIPT

Try out changing the colors, increment, and decrement Values in the below Code Playground and check the output.

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form