JavaScript Type Conversion Quiz
8 Questions
23 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which keyword is used to declare a constant in JavaScript?

  • final
  • const (correct)
  • var
  • let

What happens when you try to assign a new value to a constant in JavaScript?

  • It throws a ReferenceError
  • It throws a TypeError (correct)
  • It throws a SyntaxError
  • It allows the new assignment silently

In JavaScript, which data type declaration can not be changed once initialized?

  • 'final' variables
  • 'const' variables (correct)
  • 'let' variables
  • 'var' variables

What will be the output of console.log(typeof(str)) after executing the given code snippet?

<p>'string' (A)</p> Signup and view all the answers

Which method is used for explicit type conversion from number to string in JavaScript?

<p>.String() (D)</p> Signup and view all the answers

What error will occur if you try to declare a 'const' without initializing it?

<p>SyntaxError: Missing initializer (B)</p> Signup and view all the answers

What would be the outcome of executing the function fun() defined in the text?

<p>'testing' and 1.1 printed on separate lines (A)</p> Signup and view all the answers

How does JavaScript handle variable declarations without 'let' or 'var'?

<p>Automatically assigns them as global variables (D)</p> 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);

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 and var 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.

Quiz Team

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.

More Like This

Use Quizgecko on...
Browser
Browser