Podcast
Questions and Answers
Which keyword is used to declare a constant in JavaScript?
Which keyword is used to declare a constant in JavaScript?
What happens when you try to assign a new value to a constant in JavaScript?
What happens when you try to assign a new value to a constant in JavaScript?
In JavaScript, which data type declaration can not be changed once initialized?
In JavaScript, which data type declaration can not be changed once initialized?
What will be the output of console.log(typeof(str)) after executing the given code snippet?
What will be the output of console.log(typeof(str)) after executing the given code snippet?
Signup and view all the answers
Which method is used for explicit type conversion from number to string in JavaScript?
Which method is used for explicit type conversion from number to string in JavaScript?
Signup and view all the answers
What error will occur if you try to declare a 'const' without initializing it?
What error will occur if you try to declare a 'const' without initializing it?
Signup and view all the answers
What would be the outcome of executing the function fun() defined in the text?
What would be the outcome of executing the function fun() defined in the text?
Signup and view all the answers
How does JavaScript handle variable declarations without 'let' or 'var'?
How does JavaScript handle variable declarations without 'let' or 'var'?
Signup and view all the answers
Study Notes
Type Conversion: Explicit
- Number to string:
- Using global method
String()
:console.log(String(num))
- Concatenating number with an empty string:
let str = "" + num
- Using
toString()
:let num = 1; let str = num.toString();
- Using
toString(base)
:let num = 10; let str = num.toString(8);
- Using global method
String to a Number
- Using unary
+
operator:let str = "1.11"; let num = +str;
- Using
parseInt()
:let str = "1.11"; let num = parseInt(str);
- Using
parseFloat()
:let str = "1.0px"; let num = parseFloat(str);
Float to an Integer
- Using
parseInt()
:let flt = 1.11; let num = parseInt(flt);
- Using
Math.round()
:let flt = 1.11; let num = Math.round(flt);
- Using
Math.ceil()
:let flt = 1.11; let num = Math.ceil(flt);
- Using
Math.floor()
:let flt = 1.11; let num = Math.floor(flt);
Variables and Constants
-
let
andvar
keywords are used to declare variables -
const
keyword is used to declare constants - Constants cannot be reassigned, and must be initialized during declaration
- The value of a constant cannot be changed, but the content of the value can be changed
Comments
- Comments are similar to those in C, C++, and Java
- Comments can be written between
//
and the end of the line - Java-style comments (`/** */) are treated as regular comments in JavaScript
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge on JavaScript type conversion with this quiz covering topics such as converting numbers to strings, using different bases with toString, and converting strings to numbers using in-built functions like unary + operator and parseInt.